在Entity Framework 6.1中添加列-多对多-代码优先

克林特·伊斯特伍德

举例来说,我正在使用本文在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");
       });
  }
}
八叶球菌

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

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代替数据注释,则可以使用以下配置:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Entity Framework 6中删除多对多关系

如何在代码优先的Entity Framework流利API中指定多列UNIQUE约束

跟踪Entity Framework中与行为多对多关系的更改

移植现有的代码优先模型,并将流畅的aPI配置从Entity Framework 6移植到Entity Framework Core 2.0

Entity Framework代码优先:如何在模型上添加更多道具并更新数据库?

Entity Framework Core 2.0中的多对多连接

在Entity Framework核心中更新多对多

SQL Server上的Entity Framework 6代码优先:将“布尔”映射为“数字(1,0)”而不是“位”

更新Entity Framework Core中的多对多关系

具有InverseProeprty的同一集合Entity Framework 6上的多对多和一对多

如何在Entity Framework Core中配置多对多关系

在链接表中多对多插入Entity Framework

我的数据库不是通过Entity Framework 6代码优先方法自动创建的吗?

在实体框架6代码优先方法中在两个表之间创建一对多和多对一映射

如何使用Entity Framework代码优先从数据库中删除所有相关实体

在Entity Framework中以多对多关系更新记录

Entity Framework 5.0代码首先进行多对多更新

带有代码优先EF6的多租户

EntityFramwork多对多代码优先

共享数据上下文/注入依赖项Entity Framework 6代码优先

首先在同一张表中启用Entity Framework 5代码中的多对多

具有多对多关系的Entity Framework代码优先方法为SQL Server播种

代码优先EF6-更新MVC Web应用程序中的多对多关系

合并多个迁移Entity Framework代码优先

Entity Framework Core - 多对多“影子查询”

在 Entity Framework C# 中添加多对多对象

如何使用 db first 方法在 Entity Framework 中编写多对多查询?

Entity Framework Core 在多对多添加新条目时尝试添加旧条目

.NET 6 Entity Framework Core:多对多关系