这次开发项目,我依然做的是.Net,前几个月的项目底层设计使用的的是 ORM 思想,技术选用的是NHibernate,这次也不例外,开发.Net项目,依然使用的是ORM的思想,不同的是这次开发技术选用的是EF(EntityFrameWork)。这个框架可是让我费眼不少,我了解它,从它的XML开始的。开始说说有关EF中xml的解读。
一、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,先看图:
这三层的功能分别是:
<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相关连的作用。 |
||
<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><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 |
关系属性 |
||
Entity Framework的核心 – EDM(Entity Data Model) 一
原文:http://blog.csdn.net/wangyongxia921/article/details/42061695