【发布时间】:2016-10-19 15:30:25
【问题描述】:
我正在尝试使用一些 Entity Framework Code First 模型设置一些导航属性。我希望它们看起来像这个例子:
public class Course
{
[Key]
public int CourseId { get; set; }
public string CourseName { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
public class Student
{
[Key]
public int StudentId { get; set; }
public string StudentName { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
public class StudentCourses
{
[Key, Column(Order = 0)]
public int StudentId { get; set; }
public virtual Student Student { get; set; }
[Key, Column(Order = 1)]
public int CourseId { get; set; }
public virtual Course Course { get; set; }
}
因此,StudentCourses 表中将建立 Student 和 Course 关系。学生类的实例将自动引用所有学生的课程,反之亦然,课程类的实例将自动引用其所有学生。 StudentCourses 类的一个实例会自动引用它的 Student 和 Course。但是当我尝试更新数据库时,这些关系似乎没有得到正确解释。我在这里有什么遗漏吗?也许需要在上下文类中进行一些配置?导航属性的大多数示例仅显示一对多关系导航属性。
【问题讨论】:
标签: c# asp.net entity-framework ef-code-first code-first