c# - 在 Entity Framework 6.1 中添加列 - 多对多 - Code First

标签 c# entity-framework ef-code-first many-to-many code-first

举个例子,我在 codeproject 上使用这篇文章中的代码:http://www.codeproject.com/Articles/234606/Creating-a-Many-To-Many-Mapping-Using-Code-First

enter image description here

我试图在我的代码中做一些类似的事情,但我还想要一个数量属性,所以:

  1. 一个类(class)可以有很多人
  2. 一个人可以有很多门类(class)
  3. 每个人都可以选择每门类(class)的数量。 (我知道两次选择相同的类(class)没有意义,但这只是一个例子,或者用汉堡包类型替换类(class):-))

我想我必须在 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");
       });
  }
}

最佳答案

要将列添加到联结表,您需要将其显式映射为实体并创建两个一对多关系:

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);

关于c# - 在 Entity Framework 6.1 中添加列 - 多对多 - Code First,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30964620/

相关文章:

c# - 为什么 MySQL 数据库中的 Code First 没有创建模型 connectionString?

performance - 在 Entity Framework LINQ 查询中使用 IEnumerable.Contains 时如何避免查询计划重新编译?

entity-framework - EF Code First 多个实体到同一个表

c# - Entity Framework DbContext SaveChanges() OriginalValue 不正确

c# - 检查玩家是否通过了两个门

c# - HTML Agility Pack - 只能从文件系统加载 xml 文档,不能从 Web

c# - 需要帮助将包含空格的 bbcode URL 转换为有效的 Markdown

c# - 测试 Entity Framework 查找方法

entity-framework - EF Code First - 多个应用程序版本共享数据库

c# - Telerik WebAii Framework - 如何将焦点更改为新打开的页面/窗口