【问题标题】:Entity framework conventions making unexpected tables制作意外表的实体框架约定
【发布时间】:2013-07-10 11:24:50
【问题描述】:

我正在为一个电子学习系统建模,用户可以通过该系统注册课程。一门课程可以由一系列内容或一系列课程部分组成,这些部分又包含内容。

我的模型定义如下:

public class Course
{
    [Key]
    public int CourseID { get; set; }

    //1 to many relationship
    public virtual ICollection<Content> Contents { get; set; }

    public virtual ICollection<CourseSection> CourseSections { get; set; }

    //many to many relationship
    public virtual ICollection<User> Users { get; set; }
}

public class CourseSection
{
    [Key]
    public int CourseSectionID { get; set; }

    public int CourseID { get; set; }

    public virtual ICollection<Content> Contents { get; set; }
}

public class Content
{
    [Key]
    public int ContentID { get; set; } 

    //many-to-many mappings
    public virtual ICollection<Course> Courses { get; set; }

    public virtual ICollection<CourseSection> CourseSections { get; set; }
}

初始化上下文后,我可以看到在数据库中创建了两个与我创建的模型无关的表。以下两个表是意外创建的,我想了解原因:

ContentCourse: Content_ContentID、Course_CourseID

CourseSectionContent:CourseSection_CourseSectionID、Content_ContentID

当我注意到正在创建这两个表时,我决定通过添加以下两个模型来为 CourseContentSectionContent 创建映射表:

public class CourseContent
{
    [Key, Column(Order = 0)]
    public int CourseID { get; set; }

    [Key, Column(Order = 1)]
    public int ContentID { get; set; }
}

public class SectionContent
{
    [Key, Column(Order = 0)]
    public int CourseSectionID { get; set; }

    [Key, Column(Order = 1)]
    public int ContentID { get; set; }   
}

不幸的是,这些表仍在创建中。谁能从我的代码中看出我哪里出错了?任何帮助将不胜感激。

【问题讨论】:

    标签: database-design entity-framework-5 conventions fluent


    【解决方案1】:

    您说的是一对多关系 (Course.Contents),但事实并非如此。 CourseContent 具有多对多关系。

    因此,表没有创建意外。创建它们是因为在关系数据库中建模多对多关联的唯一方法是创建联结表。您会注意到,当您将现有的 Content 项目添加到例如Course.Contents,EF 将在ContentCourse 中插入记录,而不是在CourseContent 中。

    您自己定义的类与您的类模型中的任何其他类都不相关。因此,EF 不会为它们创建表,如果这样做,它们将只是断开连接的表。

    如果您决定将联结表合并到您的概念模型(= 类模型)中,则该模型必须进行相当大的更改。例如CourseContent

    public class CourseContent
    {
        [Key, Column(Order = 0)]
        public int CourseID { get; set; }
        [ForeignKey("CourseID")]
        public virtual Course Course { get; set; }
    
        [Key, Column(Order = 1)]
        public int ContentID { get; set; }
        [ForeignKey("ContentID ")]
        public virtual Content Content { get; set; }
    }
    
    public class Course
    {
        [Key]
        public int CourseID { get; set; }
    
        //1 to many relationship (now it is)
        public virtual ICollection<CourseContent> Contents { get; set; }
    
        ...
    }
    
    public class Content
    {
        [Key]
        public int ContentID { get; set; } 
    
        //many-to-many mappings
        public virtual ICollection<CourseContent> Courses { get; set; }
    
        ...
    }
    

    如果您想保留多对多关联,但又想控制联结表的名称,您可以使用流畅的映射,如this answer

    【讨论】:

    • 干杯 Gert 好人帮助了我 :) 知道我在某处错过了关键一步!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多