Entity Framework的核心 – EDM(Entity Data Model) 一
2016-02-05 09:19
465 查看
/article/1540050.html
一、EnityFramework
EnityFramework的全程是ADO.NET Entity Framework 。和Nhibernate一样,EF
同样是遵守ORM的思想,利用了抽象化数据结构的方式,将每个数据库对象都转换成应用程序对象 (entity),而数据字段都转换为属性
(property),关系则转换为结合属性 (association),让数据库的 E/R
模型完全的转成对象模型,如此让程序设计师能用最熟悉的编程语言来调用访问。
EF是如何来实现这个原理的呢?
EF中存在一个主要的文件:*.edm 。这就是EF的核心。EF以EDM( Entity Data Model
)
为主,将数据逻辑层切分为三块,分别为 Conceptual Schema, Mapping Schema 与 Storage Schema
三层,其上还有 Entity Client,Object Context 以及 LINQ 可以使用,今天咱们讨论的是EDM,先看图:
![](http://img.blog.csdn.net/20141221174909461?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ3lvbmd4aWE5MjE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
这三层的功能分别是:
(1) 概念层:负责向上的对象与属性显露与访问。
(2) 对应层:将上方的概念层和底下的储存层的数据结构对应在一起。
(3) 储存层:依不同数据库与数据结构,而显露出实体的数据结构体,和 Provider 一起,负责实际对数据库的访问和 SQL 的产生。这样协同工作让数据访问的工作得以顺利与完整的进行。
二、edm对应的XML
edm 有三层,与之对应的xml也有三层:csdl(Conceptual Schema Definition
Language),msl(Mapping Specification Language),ssdl(Storage Schema
Definition Language)。
1、CSDL基本结构
这个文件完全以程序语言的角度来定义模型的概念。即其中定义的实体、主键、属性、关联等都是对应于.NET Framework中的类型。下面是csdl的基本结构,没有实体关联:
[html] view plaincopyprint?
<EntityContainer Name="EmployeesContext">
<EntitySet Name="Employees" EntityType="Employees.Employees" />
</EntityContainer>
<EntityType Name="Employees">
<Key>
<PropertyRef Name="EmployeeId" />
</Key>
<Property Name="EmployeeId" Type="Guid" Nullable="false" />
<Property Name="LastName" Type="String" Nullable="false" />
<Property Name="FirstName" Type="String" Nullable="false" />
<Property Name="Email" Type="String" Nullable="false" />
</EntityType>
每个节点含义如下;
注意:如果 该实体有与其他实体关联,有如下变动:
(1):那么在EntityContainer中就会多出一个Association节点,Association节,这是真正定义关系的地方。看如下实例:
[html] view plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
<EntityContainer Name="ITOO_UIEntities" annotation:LazyLoadingEnabled="true">
<EntitySet Name="Controls" EntityType="ITOO_UIModel.Controls" />
<EntitySet Name="NonQueryProperties" EntityType="ITOO_UIModel.NonQueryProperties" />
<EntitySet Name="QueryProperties" EntityType="ITOO_UIModel.QueryProperties" />
<AssociationSet Name="ControlsQueryProperties" Association="ITOO_UIModel.ControlsQueryProperties">
<End Role="Controls" EntitySet="Controls" />
<End Role="QueryProperties" EntitySet="QueryProperties" />
</AssociationSet>
<<span style="color:#FF0000;">AssociationSet</span> Name="ControlsNonQueryProperties" Association="ITOO_UIModel.ControlsNonQueryProperties">
<End Role="Controls" EntitySet="Controls" />
<End Role="NonQueryProperties" EntitySet="NonQueryProperties" />
</AssociationSet>
</EntityContainer>
(2)EntityType节点中增加:NavigationProperty节点
[html] view plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
<EntityType Name="NonQueryProperties">
<Key>
<PropertyRef Name="NonQueryId" />
</Key>
<Property Name="NonQueryId" Type="Guid" Nullable="false" />
<Property Name="PropertyName" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="true" />
<Property Name="PropertyDesc" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="true" />
<Property Name="ControlHtmlName" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="true" />
<Property Name="ControlHtmlId" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="true" />
<Property Name="IsNecessary" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="true" />
<Property Name="IsShow" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="true" />
<Property Name="EntityName" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="true" />
<Property Name="EntityDesc" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="true" />
<NavigationProperty Name="Controls" Relationship="ITOO_UIModel.ControlsNonQueryProperties" FromRole="NonQueryProperties" ToRole="Controls" />
</EntityType>
具体节点的含义如下:
这就是基本的CSDL ,你在edm中添加一个实体,用模型画出来的,然后通过打开XML方式来查看,对比XML中的csdl ,你会发现两者一样。
下篇继续:edm中ssdl。
一、EnityFramework
EnityFramework的全程是ADO.NET Entity Framework 。和Nhibernate一样,EF
同样是遵守ORM的思想,利用了抽象化数据结构的方式,将每个数据库对象都转换成应用程序对象 (entity),而数据字段都转换为属性
(property),关系则转换为结合属性 (association),让数据库的 E/R
模型完全的转成对象模型,如此让程序设计师能用最熟悉的编程语言来调用访问。
EF是如何来实现这个原理的呢?
EF中存在一个主要的文件:*.edm 。这就是EF的核心。EF以EDM( Entity Data Model
)
为主,将数据逻辑层切分为三块,分别为 Conceptual Schema, Mapping Schema 与 Storage Schema
三层,其上还有 Entity Client,Object Context 以及 LINQ 可以使用,今天咱们讨论的是EDM,先看图:
这三层的功能分别是:
(1) 概念层:负责向上的对象与属性显露与访问。
(2) 对应层:将上方的概念层和底下的储存层的数据结构对应在一起。
(3) 储存层:依不同数据库与数据结构,而显露出实体的数据结构体,和 Provider 一起,负责实际对数据库的访问和 SQL 的产生。这样协同工作让数据访问的工作得以顺利与完整的进行。
二、edm对应的XML
edm 有三层,与之对应的xml也有三层:csdl(Conceptual Schema Definition
Language),msl(Mapping Specification Language),ssdl(Storage Schema
Definition Language)。
1、CSDL基本结构
这个文件完全以程序语言的角度来定义模型的概念。即其中定义的实体、主键、属性、关联等都是对应于.NET Framework中的类型。下面是csdl的基本结构,没有实体关联:
[html] view plaincopyprint?
<EntityContainer Name="EmployeesContext">
<EntitySet Name="Employees" EntityType="Employees.Employees" />
</EntityContainer>
<EntityType Name="Employees">
<Key>
<PropertyRef Name="EmployeeId" />
</Key>
<Property Name="EmployeeId" Type="Guid" Nullable="false" />
<Property Name="LastName" Type="String" Nullable="false" />
<Property Name="FirstName" Type="String" Nullable="false" />
<Property Name="Email" Type="String" Nullable="false" />
</EntityType>
每个节点含义如下;
EntityContainer | ||
Name | EntityContainer的名称,其将作为产生的ObjectContext类的名称 | |
EntitySet | ||
Name | ObjectContext内与此Entity类型对应的属性名 | |
EntityType | ObjectContext内与此Entity类型对应的属性的类型 | |
AssociationSet | ||
End | 有两个End子节点,分别描述建立此关系的两个EntitySet | |
Role | 对应到Association中End节的Role属性,起到将AssociationSet与Association相关连的作用。 |
(1):那么在EntityContainer中就会多出一个Association节点,Association节,这是真正定义关系的地方。看如下实例:
[html] view plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
<EntityContainer Name="ITOO_UIEntities" annotation:LazyLoadingEnabled="true">
<EntitySet Name="Controls" EntityType="ITOO_UIModel.Controls" />
<EntitySet Name="NonQueryProperties" EntityType="ITOO_UIModel.NonQueryProperties" />
<EntitySet Name="QueryProperties" EntityType="ITOO_UIModel.QueryProperties" />
<AssociationSet Name="ControlsQueryProperties" Association="ITOO_UIModel.ControlsQueryProperties">
<End Role="Controls" EntitySet="Controls" />
<End Role="QueryProperties" EntitySet="QueryProperties" />
</AssociationSet>
<<span style="color:#FF0000;">AssociationSet</span> Name="ControlsNonQueryProperties" Association="ITOO_UIModel.ControlsNonQueryProperties">
<End Role="Controls" EntitySet="Controls" />
<End Role="NonQueryProperties" EntitySet="NonQueryProperties" />
</AssociationSet>
</EntityContainer>
(2)EntityType节点中增加:NavigationProperty节点
[html] view plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
<EntityType Name="NonQueryProperties">
<Key>
<PropertyRef Name="NonQueryId" />
</Key>
<Property Name="NonQueryId" Type="Guid" Nullable="false" />
<Property Name="PropertyName" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="true" />
<Property Name="PropertyDesc" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="true" />
<Property Name="ControlHtmlName" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="true" />
<Property Name="ControlHtmlId" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="true" />
<Property Name="IsNecessary" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="true" />
<Property Name="IsShow" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="true" />
<Property Name="EntityName" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="true" />
<Property Name="EntityDesc" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="true" />
<NavigationProperty Name="Controls" Relationship="ITOO_UIModel.ControlsNonQueryProperties" FromRole="NonQueryProperties" ToRole="Controls" />
</EntityType>
具体节点的含义如下:
EntityType | ||
Name | Entity Class的名称 | |
Abstract | 是否为抽象类 | |
BaseType | 父类 | |
Key | 主键 | |
Property | 主键之属性 | |
Name | 属性名 | |
Property | 属性 | |
Name | 属性名 | |
Type | 属性类型 | |
Nullable | 是否允许null | |
MaxLength | 属性最大长度 | |
FixLength | 是否固定长度 | |
NavigationProperty | 关系属性 |
下篇继续:edm中ssdl。
相关文章推荐
- iOS应用开发中图片的拉伸问题解决方案
- My GoogleMail c9
- notepad ++ 使用技巧
- Git活动提交常见错误总结
- Android JNI Develop 1 setup
- 不算完美的实现了自动化部署的进度实时更新
- ListView 百分比进度条(delphi版)
- 基础smart命令
- lseek Function
- angularjs中的filter(过滤器)
- lsyncd轻松同步文件
- 在互联网公司参与拍卖是一种怎样的感觉?part 1
- HDU2204 Eddy's爱好(容斥原理)
- 【剑指offer】二叉搜索树的后序遍历序列
- twig模板获取全局变量的方法
- 从零开始创建一个 PHP 扩展
- sts spring JPA(项目安装环境)
- 启动图片相关问题
- poj3070 Fibonacci (矩阵快速幂)
- 近期找工作的大学生比較多,分享以下试不可犯的错