如果我有一个Course
类,有学生的集合(ICollection<Person>
)如下:问题与的EntityFramework导航属性6
public class Person
{
public Person()
{
this.Courses = new HashSet<Course>();
}
public int PersonId { get; set; }
[Required]
public string PersonName { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
public class Course
{
public Course()
{
this.Students = new HashSet<Person>();
}
public int CourseId { get; set; }
public string CourseName { get; set; }
public virtual ICollection<Person> Students { get; set; }
}
我结束了这种结构的数据库(如预期):
(注意PersonCourses
表)
然而,在我的例子,我也想一个教练添加到课程。 这个教练也是Person
,谁可以参加课程,像其他人一样,所以我调整上述类,如下图所示:
public class Person
{
public Person()
{
this.Courses = new HashSet<Course>();
}
public int PersonId { get; set; }
[Required]
public string PersonName { get; set; }
public virtual ICollection<Course> Courses { get; set; }
public virtual ICollection<Course> InstructedCourses { get; set; }
}
public class Course
{
public Course()
{
this.Students = new HashSet<Person>();
}
public int CourseId { get; set; }
public string CourseName { get; set; }
public virtual ICollection<Person> Students { get; set; }
public virtual Person Instructor { get; set; }
}
我期待看到的是相同的数据库结构之上,但创建了一个附加的表格,将一个人链接到许多课程。
不过,我得到的是这样的:
(注意PersonCourses
表已经)
这可能是值得说明的,我没有单独的Instructor/Person
课的原因是,我期望任何Person
可以创建一个课程,从而成为一个教练t帽子课程。
首先 - 这可能通过代码优先实现在EF?我假设如此..
其次 - 我做错了什么?
第三 - 现在是周末吗?
所有帮助表示赞赏:)
可能的答案:https://stackoverflow.com/questions/19342908/how-to-create-a-many-to-many-mapping-in-entity-framework。 –