下面,我们学习Entity Framework怎么管理实体间的关系

Entity Framework支持三种关系:一对一的关系、一对多的关系、多对多的关系

前面我们创建SchoolDB的实体数据模型,下图展示了EDM可视化设计器中的实体和实体关系

 一对一的关系

上图所示,Student和StudentAddress是一对一的关系,一个学生可以有一个或零个地址,Entity Framework添加Student的导航属性到StudentAddress实体中,添加StudentAddress导航属性到Studnet实体中,StudentAddress实体有StudentId属性作为主键

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 Standard Standard { get; set; }
public virtual StudentAddress StudentAddress { get; set; }
public virtual ICollection<Course> Courses { 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; }
}

上面的代码说明,Student实体包含StudentAddress的导航属性,StudentAddress包含Student的导航属性,StudentId作为外键

一对多关系 

Standard和Teacher实体有一对多的关系,Standard可以有多个Teacher,然而Teacher只有一个Standard

为了表示这个关系,Standard实体有Teachers的集合导航属性(它是个复数)表明一个Standard可以有很多Teachers。Teacher实体有一个Standard的导航属性(不是集合)表明Teacher与一个Standard关联,它包含StandardId外键(StandardId是Standard实体的主键)

public partial class Standard
{
public Standard()
{
this.Students = new HashSet<Student>();
this.Teachers = new HashSet<Teacher>();
} public int StandardId { get; set; }
public string StandardName { get; set; }
public string Description { get; set; } public virtual ICollection<Student> Students { 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> StandardId { get; set; }
public Nullable<int> TeacherType { get; set; } public virtual ICollection<Course> Courses { get; set; } public virtual Standard Standard { get; set; }
}

上面的代码所示,Standard实体有Teacher的集合属性,因此它包含多个Teacher对象,(在构造函数中初始化,因此你可以添加Teacher实体,不用担心集合是否初始化)

Teacher实体包含Standard的导航属性,StandardId作为外键

多对多的关系

Student和Course是多对多的关系,一个Student可以选择多门Course,一个Course可以教多个Student

数据库中的设计StudentCourse表,包含Student和Course表的两个主键,Entity Framework不通过联合表的实体集,

Student实体中包含Course集合属性,Course实体包含Student的集合属性,

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 Standard Standard { get; set; }
public virtual StudentAddress StudentAddress { 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 System.Data.Entity.Spatial.DbGeography Location { get; set; }
public Nullable<int> TeacherId { get; set; } public virtual Teacher Teacher { get; set; }
public virtual ICollection<Student> Students { get; set; }
}

备注:Entity Framework支持多对多的关系,当联合表(StudentCourse)不包含其他列(除了关联两个表的主键外)。如果联合表包含额外的列,例如DateCreated,EDM将给中间表创建实体,你不得不手动管理多对多的CRUD操作。

打开EDM的Xml文件,你讲看到存储模型有StudentCourse实体集,而概念模型中没有,在MSL(C-S Mapping),它已经实现Student和Course之间的映射

由此,多对多的关系在EDM中被C-S Mapping管理,当你在Course中添加Student或在Student中添加Course保存到数据库中,它将在StudentCourse表中插入Student和Course的主键

这种映射不仅使两个实体方便的关联,而且通过这个联合管理查询、添加、更新操作。

实体图表

当一个实体与其他实体有关联,对象的所有级层关系通过图表展示出来。例如,下面是Student实体图表,

EntityFramework 学习 一 Entity Relationships 实体的关系的更多相关文章

  1. EntityFramework 学习 一 Entity Lifecycle 实体生命周期

    当我们执行CRUD(Create,Read,Update,Delete)操作之前,最重要的是搞明白实体的生命周期和EntityFrameword怎么管理实体 在一个实体的生命周期里,每个实体都有一个实 ...

  2. EntityFramework 学习 一 Entity Framework结构体系

    Entity Framework 架构 EDM(Entity Data Model)EDM由3个主要部分组成 Conceptual model , Mapping and Storage model. ...

  3. EntityFramework 学习 一 Entity Framework 查询设计

    First/FirstOrDefault: using (var ctx = new SchoolDBEntities()) { var student = (from s in ctx.Studen ...

  4. EntityFramework 学习 一 创建实体数据模型 Create Entity Data Model

    1.用vs2012创建控制台程序 2.设置项目的.net 版本 3.创建Ado.net实体数据模型 3.打开实体数据模型向导Entity Framework有四种模型选择 来自数据库的EF设计器(Da ...

  5. Entity Framework 实体框架的形成之旅--基于泛型的仓储模式的实体框架(1)

    很久没有写博客了,一些读者也经常问问一些问题,不过最近我确实也很忙,除了处理日常工作外,平常主要的时间也花在了继续研究微软的实体框架(EntityFramework)方面了.这个实体框架加入了很多特性 ...

  6. Entity Framework 实体框架的形成之旅--实体数据模型 (EDM)的处理(4)

    在前面几篇关于Entity Framework 实体框架的介绍里面,已经逐步对整个框架进行了一步步的演化,以期达到统一.高效.可重用性等目的,本文继续探讨基于泛型的仓储模式实体框架方面的改进优化,使我 ...

  7. MVC5 Entity Framework学习之Entity Framework高级功能(转)

    在之前的文章中,你已经学习了如何实现每个层次结构一个表继承.本节中你将学习使用Entity Framework Code First来开发ASP.NET web应用程序时可以利用的高级功能. 在本节中 ...

  8. Entity Framework 实体框架的形成之旅--实体框架的开发的几个经验总结

    在前阵子,我对实体框架进行了一定的研究,然后把整个学习的过程开了一个系列,以逐步深入的方式解读实体框架的相关技术,期间每每碰到一些新的问题需要潜入研究.本文继续前面的主题介绍,着重从整体性的来总结一下 ...

  9. EntityFramework学习

    本文档主要介绍.NET开发中两项新技术,.NET平台语言中的语言集成查询技术 - LINQ,与ADO.NET中新增的数据访问层设计技术ADO.NET Entity Framework.ADO.NET的 ...

随机推荐

  1. kafka 小案例【二】 --kafka 设置多个消费着集群

    这个配是我在http://www.cnblogs.com/zhangXingSheng/p/6646972.html 的基础上再添加的配置 设置多个消息集群 (1)复制两份配置文件 > cp c ...

  2. web.xml中url-pattern匹配规则

    小知识 一般的URL组成 URL = 服务器地址 + RequestURI 例如URI:http://localhost:8080/practice/main [http://localhost:80 ...

  3. iOS UIView添加阴影

    _bottomView.layer.masksToBounds = NO; _bottomView.backgroundColor = [UIColor whiteColor]; _bottomVie ...

  4. 题目3 : Fibonacci

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given a sequence {an}, how many non-empty sub-sequence of it ...

  5. 各种类型的电影排行榜-movie路线

    [最费脑力的14部电影]<盗梦空间>.<记忆裂痕>.<生死停留>.<死亡幻觉>.<禁闭岛>.<穆赫兰道>.<蝴蝶效应> ...

  6. Hp服务器 iLO3 使用方法

    首先iLO3 和ipmi什么关系?如下是我摘自:hp官网  的一段话 With HP iLO3, you can: Experience a fast Remote Console incorpora ...

  7. hdu1573(线性同余方程组)

    套模板,因为要是正整数,所以处理一下x=0的情况. X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  8. ACboy needs your help(简单DP)

    HDU 1712 Problem Description ACboy has N courses this term, and he plans to spend at most M days on ...

  9. echart 图表自定义样式

    initChart: function (id) { this.charts = echarts.init(document.getElementById(id)) this.charts.setOp ...

  10. config相关操作(转)

    转自:http://www.cnblogs.com/kissdodog/archive/2013/04/16/3025315.html,这是一个专题,感觉比较好,有空可以看与一下 System.Con ...