Entity Framework的核心 – EDM(Entity Data Model) 一

 这次开发项目。我依旧做的是.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。先看图:

 

  这三层的功能各自是:

      (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。

原文地址:https://www.cnblogs.com/mfrbuaa/p/5314093.html