Entity Framework的核心 – EDM(Entity Data Model) 二 SSDL&MSL

    继续上篇博客,这篇博客说的是EDM(Entity Data Model)之SSDL。


    SSDL 部分描述了:表、列、关系、主键及索引等数据库中存在的概念。查看其文档的结构SSDL与CSDL的结构基本相似。

   SSDL和CSDL之间的异同:

    1、文档结构相同

       SSDL 中同样是:EntityContainer.EntityType,Association。


    2、EntityType 的类型不同

                 其中SSDL中的EntityType使用数据库的概念的描述,CSDL中使用实体语言来描述。


    2、需要命名空间

               Namespace 都存在,这个命名空间就是我们在建立model模型的时候给出的名字。这里是:Namespace="ITOO_UIModel.Store


看下面的SSDL文档,


<span style="font-family:KaiTi_GB2312;font-size:18px;"><!-- SSDL content -->
    <edmx:StorageModels>
    <Schema Namespace="ITOO_UIModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
  <EntityContainer Name="ITOO_UIModelStoreContainer">
    <EntitySet Name="Controls" EntityType="ITOO_UIModel.Store.Controls" store:Type="Tables" Schema="dbo" />
    <EntitySet Name="NonQueryProperties" EntityType="ITOO_UIModel.Store.NonQueryProperties" store:Type="Tables" Schema="dbo" />
    <EntitySet Name="QueryProperties" EntityType="ITOO_UIModel.Store.QueryProperties" store:Type="Tables" Schema="dbo" />
    <AssociationSet Name="FK_ControlsNonQueryProperties" Association="ITOO_UIModel.Store.FK_ControlsNonQueryProperties">
      <End Role="Controls" EntitySet="Controls" />
      <End Role="NonQueryProperties" EntitySet="NonQueryProperties" />
    </AssociationSet>
    <AssociationSet Name="FK_ControlsQueryProperties" Association="ITOO_UIModel.Store.FK_ControlsQueryProperties">
      <End Role="Controls" EntitySet="Controls" />
      <End Role="QueryProperties" EntitySet="QueryProperties" />
    </AssociationSet>
  </EntityContainer>
  <EntityType Name="Controls">
    <Key>
      <PropertyRef Name="ControlId" />
    </Key>
    <Property Name="ControlId" Type="nvarchar(max)" Nullable="false" />
    <Property Name="ControlType" Type="nvarchar(max)" Nullable="false" />
    <Property Name="ControlDesc" Type="nvarchar(max)" Nullable="false" />
  </EntityType>
  <EntityType Name="NonQueryProperties">
    <Key>
      <PropertyRef Name="NonQueryId" />
    </Key>
    <Property Name="NonQueryId" Type="nvarchar(max)" Nullable="false" />
    <Property Name="PropertyName" Type="nvarchar(max)" Nullable="false" />
    <Property Name="PropertyDesc" Type="nvarchar(max)" Nullable="false" />
    <Property Name="ControlHtmlName" Type="nvarchar(max)" Nullable="false" />
    <Property Name="ControlHtmlId" Type="nvarchar(max)" Nullable="false" />
    <Property Name="IsNecessary" Type="nvarchar(max)" Nullable="false" />
    <Property Name="IsShow" Type="nvarchar(max)" Nullable="false" />
    <Property Name="EntityName" Type="nvarchar(max)" Nullable="false" />
    <Property Name="EntityDesc" Type="nvarchar(max)" Nullable="false" />
    <Property Name="Controls_ControlId" Type="nvarchar(max)" Nullable="true" />
  </EntityType>
  <EntityType Name="QueryProperties">
    <Key>
      <PropertyRef Name="QueryId" />
    </Key>
    <Property Name="QueryId" Type="nvarchar(max)" Nullable="false" />
    <Property Name="PropertyName" Type="nvarchar(max)" Nullable="false" />
    <Property Name="PropertyDesc" Type="nvarchar(max)" Nullable="false" />
    <Property Name="ControlHtmlName" Type="nvarchar(max)" Nullable="false" />
    <Property Name="ControlHtmlId" Type="nvarchar(max)" Nullable="false" />
    <Property Name="IsShow" Type="nvarchar(max)" Nullable="false" />
    <Property Name="EntityName" Type="nvarchar(max)" Nullable="false" />
    <Property Name="EntityDesc" Type="nvarchar(max)" Nullable="false" />
    <Property Name="IsCondition" Type="nvarchar(max)" Nullable="false" />
    <Property Name="Controls_ControlId" Type="nvarchar(max)" Nullable="true" />
  </EntityType>
  <Association Name="FK_ControlsNonQueryProperties">
    <End Role="Controls" Type="ITOO_UIModel.Store.Controls" Multiplicity="0..1" />
    <End Role="NonQueryProperties" Type="ITOO_UIModel.Store.NonQueryProperties" Multiplicity="*" />
    <ReferentialConstraint>
      <Principal Role="Controls">
        <PropertyRef Name="ControlId" />
      </Principal>
      <Dependent Role="NonQueryProperties">
        <PropertyRef Name="Controls_ControlId" />
      </Dependent>
    </ReferentialConstraint>
  </Association>
  <Association Name="FK_ControlsQueryProperties">
    <End Role="Controls" Type="ITOO_UIModel.Store.Controls" Multiplicity="0..1" />
    <End Role="QueryProperties" Type="ITOO_UIModel.Store.QueryProperties" Multiplicity="*" />
    <ReferentialConstraint>
      <Principal Role="Controls">
        <PropertyRef Name="ControlId" />
      </Principal>
      <Dependent Role="QueryProperties">
        <PropertyRef Name="Controls_ControlId" />
      </Dependent>
    </ReferentialConstraint>
  </Association></span>

      具体含义跟上篇博客的解释类似,不在赘述。


二、MSL

        MSL 就是CSDL 和SSDL的适配器。是可以将对象持久化到数据库的核心使者。

       MSL的根节点为Mapping,其中可以包含多个EntityContainerMapping(上例只有一个),每一个EntityContainerMapping对应着两个分别来自CSDL与SSDL的EntityContainer。这个EntityContainerMapping就是描述这两个EntityContainer间的对应。下面再给出一段代码展示EntityContainerMapping的基本格式。

     

<span style="font-family:KaiTi_GB2312;font-size:18px;"><EntityContainerMappingStorageEntityContainer=""CdmEntityContainer="">

<EntitySetMapping>

<EntityTypeMapping>

<MappingFragment>

<ScalarProperty />

</MappingFragment>

<ModificationFunctionMapping>

<InsertFunction />

<DeleteFunction />

<UpdateFunction />

</ ModificationFunctionMapping>

</EntityTypeMapping>

</EntitySetMapping>

 

<AssociationSetMapping>

<EndProperty>

<ScalarProperty />

</EndProperty>

</AssociationSetMapping>

 

<FunctionImportMapping />

</EntityContainerMapping></span>
 

     这些格式代表的具体含义如下:

   

        说明1:以上表中很重要的一个属性是MappingFragment中的StoreEntitySet属性,就像这个属性的说明中所说,其描述了CSDL的Entity对应到的SSDL的Entity的名称。这是实现下文EDM映射方案中第二条将一个概念模型的实体映射到多个存储模型的实体的关键设置。

         说明2:Contain这个元素及其属性的作用是,当多个概念模型实体映射到一个存储模型实体时,该元素的属性决定了在什么情况下一个概念模型实体映射到指定的存储模型实体。

         说明3:QueryView 元素定义概念模型中的实体与存储模型中的实体之间的只读映射。使用根据存储模型计算的 Entity SQL 查询定义此查询视图映射,并以概念模型中的实体表达结果集。同DefiningQuery定义的查询。此映射也是只读的。就是说如果想要更新此类EntitySet,也需要使用下文介绍存储过程时提到的定义更新实体的存储过程的方法,使用定义的存储过程来更新这样的EntitySet。当多对多关联在存储模型中所映射到的实体表示关系架构中的链接表时,必须为此链接表在AssociationSetMapping 元素中定义一个QueryView元素。定义查询视图时,不能在 AssociactionSetMapping 元素上指定 StorageSetName 属性。定义查询视图时,AssociationSetMapping 元素不能同时包含 EndProperty 映射。


    最后附上:我代码下示例:   

<span style="font-family:KaiTi_GB2312;font-size:18px;"><Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs">
  <EntityContainerMapping StorageEntityContainer="ITOO_UIModelStoreContainer" CdmEntityContainer="ITOO_UIEntities">
    <EntitySetMapping Name="Controls">
      <EntityTypeMapping TypeName="IsTypeOf(ITOO_UIModel.Controls)">
        <MappingFragment StoreEntitySet="Controls">
          <ScalarProperty Name="ControlId" ColumnName="ControlId" />
          <ScalarProperty Name="ControlType" ColumnName="ControlType" />
          <ScalarProperty Name="ControlDesc" ColumnName="ControlDesc" />
        </MappingFragment>
      </EntityTypeMapping>
    </EntitySetMapping>
    <EntitySetMapping Name="NonQueryProperties">
      <EntityTypeMapping TypeName="IsTypeOf(ITOO_UIModel.NonQueryProperties)">
        <MappingFragment StoreEntitySet="NonQueryProperties">
          <ScalarProperty Name="NonQueryId" ColumnName="NonQueryId" />
          <ScalarProperty Name="PropertyName" ColumnName="PropertyName" />
          <ScalarProperty Name="PropertyDesc" ColumnName="PropertyDesc" />
          <ScalarProperty Name="ControlHtmlName" ColumnName="ControlHtmlName" />
          <ScalarProperty Name="ControlHtmlId" ColumnName="ControlHtmlId" />
          <ScalarProperty Name="IsNecessary" ColumnName="IsNecessary" />
          <ScalarProperty Name="IsShow" ColumnName="IsShow" />
          <ScalarProperty Name="EntityName" ColumnName="EntityName" />
          <ScalarProperty Name="EntityDesc" ColumnName="EntityDesc" />
        </MappingFragment>
      </EntityTypeMapping>
    </EntitySetMapping>
    <EntitySetMapping Name="QueryProperties">
      <EntityTypeMapping TypeName="IsTypeOf(ITOO_UIModel.QueryProperties)">
        <MappingFragment StoreEntitySet="QueryProperties">
          <ScalarProperty Name="QueryId" ColumnName="QueryId" />
          <ScalarProperty Name="PropertyName" ColumnName="PropertyName" />
          <ScalarProperty Name="PropertyDesc" ColumnName="PropertyDesc" />
          <ScalarProperty Name="ControlHtmlName" ColumnName="ControlHtmlName" />
          <ScalarProperty Name="ControlHtmlId" ColumnName="ControlHtmlId" />
          <ScalarProperty Name="IsShow" ColumnName="IsShow" />
          <ScalarProperty Name="EntityName" ColumnName="EntityName" />
          <ScalarProperty Name="EntityDesc" ColumnName="EntityDesc" />
          <ScalarProperty Name="IsCondition" ColumnName="IsCondition" />
        </MappingFragment>
      </EntityTypeMapping>
    </EntitySetMapping>
    <AssociationSetMapping Name="FK_ControlsNonQueryProperties" TypeName="ITOO_UIModel.FK_ControlsNonQueryProperties" StoreEntitySet="NonQueryProperties">
      <EndProperty Name="Controls">
        <ScalarProperty Name="ControlId" ColumnName="Controls_ControlId" />
      </EndProperty>
      <EndProperty Name="NonQueryProperties">
        <ScalarProperty Name="NonQueryId" ColumnName="NonQueryId" />
      </EndProperty>
      <Condition ColumnName="Controls_ControlId" IsNull="false" />
    </AssociationSetMapping>
    <AssociationSetMapping Name="FK_ControlsQueryProperties" TypeName="ITOO_UIModel.FK_ControlsQueryProperties" StoreEntitySet="QueryProperties">
      <EndProperty Name="Controls">
        <ScalarProperty Name="ControlId" ColumnName="Controls_ControlId" />
      </EndProperty>
      <EndProperty Name="QueryProperties">
        <ScalarProperty Name="QueryId" ColumnName="QueryId" />
      </EndProperty>
      <Condition ColumnName="Controls_ControlId" IsNull="false" />
    </AssociationSetMapping>
  </EntityContainerMapping>
</Mapping></edmx:Mappings></span>


            

你可能感兴趣的:(ef,EDA)