隐藏

Entity Framework入门教程(4)---EF中的实体关系

发布:2021/7/5 9:12:20作者:管理员 来源:本站 浏览次数:1110

这一节将总结EF是怎么管理实体之间的关系。

EF与数据库一样支持三种关系类型:①一对一 ,②一对多,③多对多。


下边是一个SchoolDB数据库的实体数据模型,图中包含所有的实体和各个实体间的关系。通过设计器我们很容易看出实体间的对应关系



1.一对一


如上图,Student和StudentAddress具有一对一的关系(零或一)。一个学生只能有一个或零个地址。实体框架将Student实体导航属性添加到StudentAddress实体中,将StudentAddress实体导航属性添加到Student实体中。同时,StudentAddress类中将StudentId作为PrimaryKey和ForeignKey属性,这样Student和StudentAddress就成为一对一的关系。

复制代码


public partial class Student

{

   public Student()

   {

       this.Courses = new HashSet<Course>();

   }

   

   public int StudentID { get; set; }

   public string StudentName { get; set; }

   public Nullable<int> StandardId { get; set; }

   public byte[] RowVersion { get; set; }

   //实体导航属性

   public virtual StudentAddress StudentAddress { get; set; }

   }

   

public partial class StudentAddress

{

//同时是主键和外键

   public int StudentID { get; set; }

   public string Address1 { get; set; }

   public string Address2 { get; set; }

   public string City { get; set; }

   public string State { get; set; }

   //实体导航属性

   public virtual Student Student { get; set; }

}


复制代码


在上面的示例中,StudentId属性在StudentAddress类中是同时是PrimaryKey和ForeignKey。我们可以在context的OnModelCreating方法中使用Fluent API进行配置。

2.一对多


Standard与Teacher实体具一对多的关系。这表示多个教师评同一个级别(成绩级别A,B,C,D),而每个教师只能评一个级别,如给学生1评A级的同时不能再评一个B级。

复制代码


public partial class Standard

{

   public Standard()

   {

       this.Teachers = new HashSet<Teacher>();

   }

   

   public int StandardId { get; set; }

   public string StandardName { get; set; }

   public string Description { get; set; }

   //集合导航属性

   public virtual ICollection<Teacher> Teachers { get; set; }

}


public partial class Teacher

{

   public Teacher()

   {

       this.Courses = new HashSet<Course>();

   }

   public int TeacherId { get; set; }

   public string TeacherName { get; set; }

   public Nullable<int> TeacherType { get; set; }

   //外键

   public Nullable<int> StandardId { get; set; }

   //实体导航属性

   public virtual Standard Standard { get; set; }

}


复制代码


Standard实体具有集合导航属性 Teachers (请注意它是复数),这表明一个Standard可以有一个教师集合(老师1和老师2都可以给A级的评分)。而Teacher实体具有Standard实体导航属性,表明每个Teacher只能评一个Standard,同时Teacher中包含StandardId外键。这样Standard与Teacher就成为了一对多的关系。

3.多对多关系


Student和Course具有多到多关系。这表示一个学生可以参加许多课程,而一个课程也可以向许多学生讲授。

数据库包括StudentCourse中间表,表中只包含Student和Course的主键。

如上图所示,Student实体包含集合导航属性 Courses,Course实体包含集合导航属性 Students以表示它们之间的多对多关系。这两个表都没有外键关系,它们通过StudentCourse中间表进行关联。


下面的代码片段展示了Student和Course实体类。

复制代码


public partial class Student

{

   public Student()

   {

       this.Courses = new HashSet<Course>();

   }

   

   public int StudentID { get; set; }

   public string StudentName { get; set; }

   public Nullable<int> StandardId { get; set; }

   public byte[] RowVersion { get; set; }

   //集合导航属性

   public virtual ICollection<Course> Courses { get; set; }

}

   

public partial 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; }

}


复制代码


注:实体框架在中间表仅有两个表的主键(只有StudentId和CourseId)时才自动维护多对多关系。当我们给Student添加一个Course或给Course添加一个Student,执行SaveChange()时,EF会在中间表自动会插入对应的StudentId和CourseId。如果中间表包含其他列,那么EDM也会为中间表创建实体,EF不再自动维护中间表,那么我们就需要手动管理多对多实体的CRUD操作。