![](/img/trans.png)
[英]Code first Entity Framework (EF6) Adding Many-to-Many relationship after initial create
[英]Adding column in Entity Framework 6.1 - Many-To-Many - Code First
就像一个例子我在codeproject上使用本文中的代码: http : //www.codeproject.com/Articles/234606/Creating-a-Many-To-Many-Mapping-Using-Code-First
我试图在我的代码中做类似的事情,但我也想要一个数量属性,所以:
我想我必须在名为Quantity的PersonCourses表中添加一列,但我不知道如何在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);
如您所见,模型中没有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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.