http://blog.csdn.net/wangyongxia921/article/details/42061695
一、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的基本结构,没有实体关联:
- <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节,这是真正定义关系的地方。看如下实例:
- <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节点
- <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
|
关系属性
|
这就是基本的CSDL ,你在edm中添加一个实体,用模型画出来的,然后通过打开XML方式来查看,对比XML中的csdl ,你会发现两者一样。
下篇继续:edm中ssdl。 Entity Framework的核心 – EDM(Entity Data Model) 一
原文:http://www.cnblogs.com/wdcwy/p/5182694.html