实体框架代码优先与链接表的一对多关系映射

发布于 2024-11-30 21:40:33 字数 41 浏览 7 评论 0原文

有没有一种方法可以映射之间有链接(联接)表的两个表之间的一对多关系?

Is there a way to map one-to-many relationship between two tables that have the link (join) table in between?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱&纷飞 2024-12-07 21:40:33

从概念上讲,一对多关系只是多对多关系的一个特例。使用代码(第一种)方法,我们可以做完全相同的事情,定义多对多关系的简化版本。
使用 实体框架教程,并假设从 StudentCourse 的一对多关系,我们将有:

public class Student
{
    public Student() { }

    public int StudentId { get; set; }
    public string StudentName { get; set; }

    public virtual ICollection<Course> Courses { get; set; }
}

public class Course
{
    public Course() { }

    public int CourseId { get; set; }
    public string CourseName { get; set; }

// We don't need this side of the relationship
//  public virtual ICollection<Student> Students { get; set; }
}

...

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Student>()
                .HasMany<Course>(s => s.Courses)
                .WithMany(/* c => c.Students */)  // 'WithMany' goes empty
                .Map(c => {
                     c.MapLeftKey("Student_id");
                     c.MapRightKey("Course_id");
                     c.ToTable("StudentAndCourse");
                 });

    base.OnModelCreating(modelBuilder);
}

Conceptually, the one-to-many relationship is just a particular case of the many-to-many. Using the code (first) approach, we can do exactly the same, defining a simplified version of a many-to-many relationship.
Using the very same example used in the Entity Framework Tutorial, and assuming the one-to-many relation from Student to Course, we would have:

public class Student
{
    public Student() { }

    public int StudentId { get; set; }
    public string StudentName { get; set; }

    public virtual ICollection<Course> Courses { get; set; }
}

public class Course
{
    public Course() { }

    public int CourseId { get; set; }
    public string CourseName { get; set; }

// We don't need this side of the relationship
//  public virtual ICollection<Student> Students { get; set; }
}

...

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Student>()
                .HasMany<Course>(s => s.Courses)
                .WithMany(/* c => c.Students */)  // 'WithMany' goes empty
                .Map(c => {
                     c.MapLeftKey("Student_id");
                     c.MapRightKey("Course_id");
                     c.ToTable("StudentAndCourse");
                 });

    base.OnModelCreating(modelBuilder);
}
情徒 2024-12-07 21:40:33

为什么要为一对多关系建立一个联接表?您所需要的只是从一个到另一个的外键。仅对于多对多关系才需要连接表。

Why would you have a join table for a one-to-many relationship? All you need is a foreign key from one to the other. A join table is only necessary for many-to-many relationships.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文