在Entity Framework 6.1中添加列 - 多对多关系 - Code First

5
作为示例,我使用了codeproject上这篇文章中的代码:http://www.codeproject.com/Articles/234606/Creating-a-Many-To-Many-Mapping-Using-Code-First 我试图在我的代码中做类似的事情,但我还想要一个数量属性:
1. 一个课程可以有多个人 2. 一个人可以有多个课程 3. 每个人可以选择每门课程的数量。(我知道选择同一门课程两次没有意义,但这只是一个例子,或者将课程替换为汉堡的类型:-))
我想我必须在PersonCourses表中添加一个名为Quantity的列,但我不知道如何在Code First中实现。
代码:
public class Person
{
  public int PersonId { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }

  public ICollection<Course> CoursesAttending { get; set; }

  public Person()
  {
    CoursesAttending = new HashSet<Course>();
  }
}

public class Course
{
  public int CourseId { get; set; }
  public string Title { get; set; }

  public ICollection<Person> Students { get; set; }

  public Course()
  {
    Students = new HashSet<Person>();
  }
}

public class SchoolContext : DbContext
{
  public DbSet<Course> Courses { get; set; }
  public DbSet<Person> People { get; set; }

  public SchoolContext()
    : base("MyDb")
  {
  }
}

背景:

public class SchoolContext : DbContext
{
  public DbSet<Course> Courses { get; set; }
  public DbSet<Person> People { get; set; }

  public SchoolContext()
    : base("MyDb")
  {
  }

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Entity<Course>().
      HasMany(c => c.Students).
      WithMany(p => p.CoursesAttending).
      Map(
       m =>
       {
         m.MapLeftKey("CourseId");
         m.MapRightKey("PersonId");
         m.ToTable("PersonCourses");
       });
  }
}

只需在您的课程表中添加一个int变量。如果您不想清除数据库,则必须进行迁移。您可以参考此链接:http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application - yash
2个回答

5
要在联接表中添加列,您需要将其映射为实体,并创建两个一对多关系:
public class PersonCourse
{
   [Key, Column(Order = 0),ForeignKey("Person")]
   public int PersonId { get; set; }
   [Key, Column(Order = 1),ForeignKey("Course")]
   public int CourseId { get; set; }

   public Person Person { get; set; }
   public Course Course { get; set; }

   public int Quantity{ get; set; }
}

public class Person
{
  public int PersonId { get; set; }
  //...

  public ICollection<PersonCourse> CoursesAttending { get; set; }

  public Person()
  {
    CoursesAttending = new HashSet<PersonCourse>();
  }
}

public class Course
{
  public int CourseId { get; set; }
  //...

  public ICollection<PersonCourse> Students { get; set; }

  public Course()
  {
    Students = new HashSet<PersonCourse>();
  }
}

如果您想使用Fluent Api而不是Data Annotations,可以使用以下配置:
modelBuilder.Entity<PersonCourse>().HasKey(pc => new { pc.PersonId, pc.CourseId});

modelBuilder.Entity<PersonCourse>().HasRequired(pc=>pc.Person).WithMany(p=>p.CoursesAttending).HasForeignKey(pc=>pc.PersonId);

modelBuilder.Entity<PersonCourse>().HasRequired(pc=>pc.Course).WithMany(c=>c.Students).HasForeignKey(pc=>pc.CourseId);

2

正如您所看到的,在您的模型中没有PersonCourses表,因为您使用了EF的多对多功能(EF将自行创建它)。如果您想向PersonCourses表添加一些自己的列,则不应使用EF的多对多功能,而应使用两个一对多关系。

将您的模型更改为以下内容:

public class Person
{
  public int PersonId { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }

  public ICollection<PersonCourse> PersonCourses { get; set; }
}    
public class Course
{
  public int CourseId { get; set; }
  public string Title { get; set; }

  public ICollection<PersonCourse> PersonCoursess { get; set; }
}
public class PersonCourse {
    [Key, Column(Order = 0)]
    public int PersonId { get; set; }
    [Key, Column(Order = 1)]
    public int CourseId { get; set; }    
    public int Quantity { get; set; }
    public Person Person { get; set; }
    public Course Course { get; set; }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接