Entity Framework Code First 导航 属性 通过关系 table
Entity Framework Code First navigation property through relational table
我正在尝试使用一些 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 table 中建立。学生 class 的实例将自动引用该学生的所有课程,反之亦然,课程 class 的实例将自动引用其所有学生。 StudentCourses class 的一个实例会自动引用它的 Student 和 Course。但是当我尝试更新数据库时,这些关系似乎没有得到正确的解释。我在这里缺少什么吗?也许需要在上下文 class 中进行一些配置?大多数导航属性示例仅显示一对多关系导航属性。
当您有 M : M
关系时,您需要如下所示构建模型。您不需要构建连接 table。 EF会在你做数据迁移的时候为你创建一个。
使用约定的模型配置。
public class Student
{
public Student()
{
this.Courses = new HashSet<Course>();
}
public int StudentId { get; set; }
public string StudentName { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
public class Course
{
public Course()
{
this.Students = new HashSet<Student>();
}
public int CourseId { get; set; }
public string CourseName { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
你的上下文class应该是这样的。
public class YourDBContext : DBContext
{
public YourDBContext () : base("Default")
{
}
public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
在听取了 Sampath 的建议后,我实际上决定要为该关系附加一些其他属性。所以我最终定义了 StudentCourses class 毕竟是这样的:
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; }
public int Grade { get; set; }
}
所以我像这样更改了学生和课程:
public class Course
{
[Key]
public int CourseId { get; set; }
public string CourseName { get; set; }
public virtual ICollection<StudentCourses> Students { get; set; }
}
public class Student
{
[Key]
public int StudentId { get; set; }
public string StudentName { get; set; }
public virtual ICollection<StudentCourses> Courses { get; set; }
}
最重要的是,我没有将 StudentCourses 添加到 DbContext。因此,在执行 Update-Database 之后,EF 自动为 StudentCourses 创建了 table,并且导航属性都有效。
我正在尝试使用一些 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 table 中建立。学生 class 的实例将自动引用该学生的所有课程,反之亦然,课程 class 的实例将自动引用其所有学生。 StudentCourses class 的一个实例会自动引用它的 Student 和 Course。但是当我尝试更新数据库时,这些关系似乎没有得到正确的解释。我在这里缺少什么吗?也许需要在上下文 class 中进行一些配置?大多数导航属性示例仅显示一对多关系导航属性。
当您有 M : M
关系时,您需要如下所示构建模型。您不需要构建连接 table。 EF会在你做数据迁移的时候为你创建一个。
使用约定的模型配置。
public class Student
{
public Student()
{
this.Courses = new HashSet<Course>();
}
public int StudentId { get; set; }
public string StudentName { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
public class Course
{
public Course()
{
this.Students = new HashSet<Student>();
}
public int CourseId { get; set; }
public string CourseName { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
你的上下文class应该是这样的。
public class YourDBContext : DBContext
{
public YourDBContext () : base("Default")
{
}
public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
在听取了 Sampath 的建议后,我实际上决定要为该关系附加一些其他属性。所以我最终定义了 StudentCourses class 毕竟是这样的:
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; }
public int Grade { get; set; }
}
所以我像这样更改了学生和课程:
public class Course
{
[Key]
public int CourseId { get; set; }
public string CourseName { get; set; }
public virtual ICollection<StudentCourses> Students { get; set; }
}
public class Student
{
[Key]
public int StudentId { get; set; }
public string StudentName { get; set; }
public virtual ICollection<StudentCourses> Courses { get; set; }
}
最重要的是,我没有将 StudentCourses 添加到 DbContext。因此,在执行 Update-Database 之后,EF 自动为 StudentCourses 创建了 table,并且导航属性都有效。