Asp.net mvc Entityframework端口从模型优先到代码优先
我仍然在寻求将EntityFramework从模型优先移植到代码优先的实现。在Eranga的帮助下,我取得了重大进展。我遇到了另一个障碍,我无法解释是什么造成的。我有两个实体对象主题和课程Asp.net mvc Entityframework端口从模型优先到代码优先,asp.net-mvc,entity-framework,entity-framework-4,Asp.net Mvc,Entity Framework,Entity Framework 4,我仍然在寻求将EntityFramework从模型优先移植到代码优先的实现。在Eranga的帮助下,我取得了重大进展。我遇到了另一个障碍,我无法解释是什么造成的。我有两个实体对象主题和课程 一个主题可以有一门必修课 课程可以有0个或多个主题 当我执行以下linq时,它会生成wierd SQL var topics = from o in db.Topics where o.ParentTopic == null && o.Course.Id
- 一个主题可以有一门必修课
- 课程可以有0个或多个主题
var topics = from o in db.Topics where o.ParentTopic == null &&
o.Course.Id == c.Id select o;
生成的SQL是
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent1].[ShortDescription] AS [ShortDescription],
[Extent1].[LongDescription] AS [LongDescription],
[Extent1].[Property] AS [Property],
[Extent1].[Difficulty] AS [Difficulty],
[Extent1].[Weight] AS [Weight],
[Extent1].[Course_Id] AS [Course_Id],
[Extent1].[ParentTopic_Id] AS [ParentTopic_Id],
[Extent1].[Course_Id1] AS [Course_Id1]
FROM [dbo].[Topics] AS [Extent1]
WHERE ([Extent1].[ParentTopic_Id] IS NULL) AND ([Extent1].[Course_Id] = @p__linq__0)
请注意,添加了一个名为Course_Id1的字段,它不在我的对象中,也没有声明为外键。我认为在OnModelCreating()中,我已经从两个方面正确地指定了父子关系(我认为您只需要从任何一方执行),但我无法让EntityFramework不生成数据库中显然不存在的额外字段。请记住,我的数据库最初是使用ModelFirst方法创建的
有人能解释一下这个额外的字段是从哪里来的吗
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Topic
modelBuilder.Entity<Topic>()
.HasRequired(m => m.Course)
.WithMany(m=>m.Topics)
.HasForeignKey(m => m.Course_Id);
modelBuilder.Entity<Topic>()
.HasOptional(m => m.ParentTopic)
.WithMany(m => m.ChildTopics)
.HasForeignKey(m => m.ParentTopic_Id);
//////// lots of code removed for brevity. //////
modelBuilder.Entity<Course>()
.HasMany(m=>m.Topics)
.WithRequired(m => m.Course)
.HasForeignKey(m => m.Course_Id);
}
public partial class Topic
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public string ShortDescription { get; set; }
public string LongDescription { get; set; }
public string Property { get; set; }
public double? Difficulty { get; set; }
public double? Weight { get; set; }
[JsonIgnore]
public virtual Course Course { get; set; }
public int Course_Id { get; set; }
[JsonIgnore]
public virtual ICollection<Question> Questions { get; set; }
[JsonIgnore]
public virtual ICollection<Topic> ChildTopics { get; set; }
[JsonIgnore]
public virtual Topic ParentTopic { get; set; }
public int? ParentTopic_Id { get; set; }
[JsonIgnore]
public virtual ICollection<RTIQueueEntryData> RTIQueueEntryData { get; set; }
[JsonIgnore]
public virtual ICollection<Intervention> Interventions { get; set; }
[JsonIgnore]
public virtual ICollection<RtiStudentGroup> RtiStudentGroups { get; set; }
}
public partial class Course
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
public string Version { get; set; }
public string Year { get; set; }
public string ImportedId { get; set; }
[Required]
public string LocalCourseNumber { get; set; }
[Required]
public string NCESCourseNumber { get; set; }
[Required]
public string StateCourseNumber { get; set; }
public int? Grade { get; set; }
[JsonIgnore]
public virtual ICollection<Topic> PerformanceIndicators { get; set; }
[JsonIgnore]
public virtual Department Department { get; set; }
public int DepartmentId { get; set; }
[JsonIgnore]
public virtual ICollection<StudentGroup> StudentGroups { get; set; }
[JsonIgnore]
public virtual ICollection<CutPointTemplate> CutPointTemplates { get; set; }
[JsonIgnore]
public virtual School School { get; set; }
public int School_Id { get; set; }
[JsonIgnore]
public virtual ICollection<Staff> RTIStaff { get; set; }
[JsonIgnore]
public virtual ICollection<Topic> Topics { get; set; }
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
//话题
modelBuilder.Entity()
.HasRequired(m=>m.Course)
.有许多(m=>m.主题)
.HasForeignKey(m=>m.Course\u Id);
modelBuilder.Entity()
.has可选(m=>m.ParentTopic)
.WithMany(m=>m.ChildTopics)
.HasForeignKey(m=>m.ParentTopic_Id);
////////为了简洁起见,删除了大量代码//////
modelBuilder.Entity()
.HasMany(m=>m.Topics)
.WithRequired(m=>m.Course)
.HasForeignKey(m=>m.Course\u Id);
}
公共部分类主题
{
公共int Id{get;set;}
[必需]
公共字符串名称{get;set;}
公共字符串ShortDescription{get;set;}
公共字符串LongDescription{get;set;}
公共字符串属性{get;set;}
公共双难度{get;set;}
公共双权重{get;set;}
[JsonIgnore]
公共虚拟课程{get;set;}
public int Course_Id{get;set;}
[JsonIgnore]
公共虚拟ICollection问题{get;set;}
[JsonIgnore]
公共虚拟ICollection子主题{get;set;}
[JsonIgnore]
公共虚拟主题ParentTopic{get;set;}
public int?ParentTopic_Id{get;set;}
[JsonIgnore]
公共虚拟ICollection RTIQueueEntryData{get;set;}
[JsonIgnore]
公共虚拟ICollection干预{get;set;}
[JsonIgnore]
公共虚拟ICollection学生组{get;set;}
}
公共部分课程
{
公共int Id{get;set;}
[必需]
公共字符串名称{get;set;}
[必需]
公共字符串说明{get;set;}
公共字符串版本{get;set;}
公共字符串年份{get;set;}
公共字符串ImportedId{get;set;}
[必需]
公共字符串LocalCourseNumber{get;set;}
[必需]
公共字符串NCESURSENUMBER{get;set;}
[必需]
公共字符串stateCourseEnumber{get;set;}
公共整数?等级{get;set;}
[JsonIgnore]
公共虚拟ICollection性能指示器{get;set;}
[JsonIgnore]
公共虚拟部门部门{get;set;}
public int DepartmentId{get;set;}
[JsonIgnore]
公共虚拟ICollection学生组{get;set;}
[JsonIgnore]
公共虚拟ICollection切入点模板{get;set;}
[JsonIgnore]
公共虚拟学校{get;set;}
公共int学校_Id{get;set;}
[JsonIgnore]
公共虚拟ICollection{get;set;}
[JsonIgnore]
公共虚拟ICollection主题{get;set;}
}
由于此导航属性,按照惯例创建的课程
和主题
之间存在另一种关系:
public virtual ICollection<Topic> PerformanceIndicators { get; set; }
公共虚拟ICollection性能指示器{get;set;}
EF将在
主题
类中放置关系的(不可见、不公开)结尾。默认情况下,关系为一对多。因此,您在主题表(课程ID 1
)中获得了一个额外的外键属性。您完全正确,我完全应该抓住这一点。非常感谢您的快速转变。