EntityFramework 学习 一 Entity Relationships 实体的关系
下面,我们学习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 实体的关系的更多相关文章
- EntityFramework 学习 一 Entity Lifecycle 实体生命周期
当我们执行CRUD(Create,Read,Update,Delete)操作之前,最重要的是搞明白实体的生命周期和EntityFrameword怎么管理实体 在一个实体的生命周期里,每个实体都有一个实 ...
- EntityFramework 学习 一 Entity Framework结构体系
Entity Framework 架构 EDM(Entity Data Model)EDM由3个主要部分组成 Conceptual model , Mapping and Storage model. ...
- EntityFramework 学习 一 Entity Framework 查询设计
First/FirstOrDefault: using (var ctx = new SchoolDBEntities()) { var student = (from s in ctx.Studen ...
- EntityFramework 学习 一 创建实体数据模型 Create Entity Data Model
1.用vs2012创建控制台程序 2.设置项目的.net 版本 3.创建Ado.net实体数据模型 3.打开实体数据模型向导Entity Framework有四种模型选择 来自数据库的EF设计器(Da ...
- Entity Framework 实体框架的形成之旅--基于泛型的仓储模式的实体框架(1)
很久没有写博客了,一些读者也经常问问一些问题,不过最近我确实也很忙,除了处理日常工作外,平常主要的时间也花在了继续研究微软的实体框架(EntityFramework)方面了.这个实体框架加入了很多特性 ...
- Entity Framework 实体框架的形成之旅--实体数据模型 (EDM)的处理(4)
在前面几篇关于Entity Framework 实体框架的介绍里面,已经逐步对整个框架进行了一步步的演化,以期达到统一.高效.可重用性等目的,本文继续探讨基于泛型的仓储模式实体框架方面的改进优化,使我 ...
- MVC5 Entity Framework学习之Entity Framework高级功能(转)
在之前的文章中,你已经学习了如何实现每个层次结构一个表继承.本节中你将学习使用Entity Framework Code First来开发ASP.NET web应用程序时可以利用的高级功能. 在本节中 ...
- Entity Framework 实体框架的形成之旅--实体框架的开发的几个经验总结
在前阵子,我对实体框架进行了一定的研究,然后把整个学习的过程开了一个系列,以逐步深入的方式解读实体框架的相关技术,期间每每碰到一些新的问题需要潜入研究.本文继续前面的主题介绍,着重从整体性的来总结一下 ...
- EntityFramework学习
本文档主要介绍.NET开发中两项新技术,.NET平台语言中的语言集成查询技术 - LINQ,与ADO.NET中新增的数据访问层设计技术ADO.NET Entity Framework.ADO.NET的 ...
随机推荐
- STM32 寄存器库和固件库
寄存器和固件库开发的差别和联系 固件库就是函数的集合,固件库函数的作用是向下负责与寄存器直接打交道.向上提供用户函数调用的接口(API). 在 51 的开发中我们经常的作法是直接操作寄存器,比方要控制 ...
- 【问题记录】springmvc国际化问题
异常-Cannot change HTTP accept header - use a different locale resolution strategy springmvc国际化时,local ...
- 中国版Office 365混合部署功能
中国版Office 365混合部署功能已经正式上线了(原计划6月份推出),虽然支持的类型不如国际版的Office 365全面,但这也标志了该功能与之前相比,已经迈出了重要一步.目前中国版Office ...
- Python3 多线程 学习 threading
#-*- coding:utf-8 --*- #多线程测试 import time import datetime import threading def worker(): print(" ...
- html5小趣味知识点系列(一)pubdate
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- java 中 集合类相关问题
1,Java中Collection和Collections的差别 java.util.Collection 是一个集合接口.它提供了对集合对象进行基本操作的通用接口方法. Collection接口在J ...
- TextView实现打印机效果 ,字符串逐字显示
https://github.com/lygttpod/AndroidCustomView/blob/master/app/src/main/java/com/allen/androidcustomv ...
- 去除app中的标题栏
我之前一直用的是在oncreate方法中添加 requestWindowFeature(Window.FEATURE_NO_TITLE),并且必须写在setContentView(R.layout.a ...
- Jmeter查看QPS和响应时间随着时间的变化曲线
以下两个插件提供测试结果,扩展图表显示 --- Response Times Over Time --- Transactions per Second 1.打开 https://jmeter-plu ...
- web.xml配置中的log4jRefreshInterval
采用spring框架的项目如何使用log4j在spring中使用log4j,有些方便的地方, 1.动态的改变记录级别和策略,即修改log4j.properties,不需要重启web应用,这需要在web ...