Entity Framework 了解(一)

必备概念了解

(在看这篇文章之前,希望大家对于下面这三个对象的概念有些基本的理解,对象名字上有链接)

EF:Entity Framework 利用了抽象化数据结构的方式,将每个数据库对象都转换成应用程序对象 (entity),而数据字段都转换为属性 (property),关系则转换为结合属性 (association),让数据库的 E/R 模型完全的转成对象模型,如此让程序设计师能用最熟悉的编程语言来调用访问。

ORM:对象关系映射

EDM:实体数据模型

ORM是一种思想,EF是ORM思想的产物之一,而EDM是EF实现ORM思想的核心。

EF的原理

  • 在我们的项目中有一个扩展名为.edmx的文件,这就是EDM模式的表现形式。这个文件的本质是xml文件。
    Entity Framework 了解(一)_第1张图片
  • 总览XML文件
    Entity Framework 了解(一)_第2张图片
  • EDM—SSDL
    描述了表、字段、关系、主键及索引等数据库中存在的概念
    Entity Framework 了解(一)_第3张图片
<!-- SSDL content -->
    <edmx:StorageModels>
      <Schema Namespace="Model.Store" Provider="System.Data.SqlClient" ProviderManifestToken="2008" Alias="Self" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">

        <EntityType Name="BasicOnClassStudentEntities">
          <Key>
            <PropertyRef Name="OnClassStudentID" />
            <PropertyRef Name="OnClassID" />
            <PropertyRef Name="StudentID" />
          </Key>
          <Property Name="Operator" Type="varchar" MaxLength="64" />
          <Property Name="TimeStamp" Type="datetime" />
          <Property Name="IsEnabled" Type="int" />
          <Property Name="OnClassStudentID" Type="uniqueidentifier" Nullable="false" />
          <Property Name="OnClassID" Type="uniqueidentifier" Nullable="false" />
          <Property Name="StudentID" Type="uniqueidentifier" Nullable="false" />
        </EntityType>
        <EntityContainer Name="ModelStoreContainer">
          <EntitySet Name="BasicOnClassStudentEntities" EntityType="Self.BasicOnClassStudentEntities" store:Type="Tables" store:Schema="dbo">
            <DefiningQuery>SELECT 
[BasicOnClassStudentEntities].[Operator] AS [Operator], 
[BasicOnClassStudentEntities].[TimeStamp] AS [TimeStamp], 
[BasicOnClassStudentEntities].[IsEnabled] AS [IsEnabled], 
[BasicOnClassStudentEntities].[OnClassStudentID] AS [OnClassStudentID], 
[BasicOnClassStudentEntities].[OnClassID] AS [OnClassID], 
[BasicOnClassStudentEntities].[StudentID] AS [StudentID]
FROM [dbo].[BasicOnClassStudentEntities] AS [BasicOnClassStudentEntities]</DefiningQuery>
          </EntitySet>
        </EntityContainer>
      </Schema>
    </edmx:StorageModels>
Entity Type
Name 对应数据库表的名称
—Key
—-PropertyRef 外键字段
—-Name 字段名
—Property 字段
—Name 字段名
—Type 字段类型
—MaxLength 字段最大长度
—Nullable 是否允许Null

DefiningQuery定义通过实体数据模型 (EDM) 内的客户端投影映射到数据存储视图的查询

  • EDM—CSDL
    定义的实体、主键、属性、关联等都是对应于.NET Framework中的类型
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema Namespace="Model" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
        <EntityType Name="BasicOnClassStudentEntities">
          <Key>
            <PropertyRef Name="OnClassStudentID" />
            <PropertyRef Name="OnClassID" />
            <PropertyRef Name="StudentID" />
          </Key>
          <Property Name="Operator" Type="String" MaxLength="64" FixedLength="false" Unicode="false" />
          <Property Name="TimeStamp" Type="DateTime" Precision="3" />
          <Property Name="IsEnabled" Type="Int32" />
          <Property Name="OnClassStudentID" Type="Guid" Nullable="false" />
          <Property Name="OnClassID" Type="Guid" Nullable="false" />
          <Property Name="StudentID" Type="Guid" Nullable="false" />
        </EntityType>
        <EntityContainer Name="Entities" annotation:LazyLoadingEnabled="true">
          <EntitySet Name="BasicOnClassStudentEntities" EntityType="Self.BasicOnClassStudentEntities" />
        </EntityContainer>
      </Schema>
    </edmx:ConceptualModels>
  • EDM—MSL
    将上面的SSDL和CSDL对应,主要包括CSDL属性与SSDL中列的对应
    <!-- C-S mapping content -->
    <edmx:Mappings>
      <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
        <EntityContainerMapping StorageEntityContainer="ModelStoreContainer" CdmEntityContainer="Entities">
          <EntitySetMapping Name="BasicOnClassStudentEntities">
            <EntityTypeMapping TypeName="Model.BasicOnClassStudentEntities">
              <MappingFragment StoreEntitySet="BasicOnClassStudentEntities">
                <ScalarProperty Name="Operator" ColumnName="Operator" />
                <ScalarProperty Name="TimeStamp" ColumnName="TimeStamp" />
                <ScalarProperty Name="IsEnabled" ColumnName="IsEnabled" />
                <ScalarProperty Name="OnClassStudentID" ColumnName="OnClassStudentID" />
                <ScalarProperty Name="OnClassID" ColumnName="OnClassID" />
                <ScalarProperty Name="StudentID" ColumnName="StudentID" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
        </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>
  </edmx:Runtime>

说明1:以上表中很重要的一个属性是MappingFragment中的StoreEntitySet属性,就像这个属性的说明中所说,其描述了CSDL的Entity对应到的SSDL的Entity的名称。这是实现下文EDM映射方案中第二条将一个概念模型的实体映射到多个存储模型的实体的关键设置。
说明2:Contain这个元素及其属性的作用是,当多个概念模型实体映射到一个存储模型实体时,该元素的属性决定了在什么情况下一个概念模型实体映射到指定的存储模型实体。
说明3:QueryView 元素定义概念模型中的实体与存储模型中的实体之间的只读映射。使用根据存储模型计算的 Entity SQL 查询定义此查询视图映射,并以概念模型中的实体表达结果集。同DefiningQuery定义的查询。此映射也是只读的。就是说如果想要更新此类EntitySet,也需要使用下文介绍存储过程时提到的定义更新实体的存储过程的方法,使用定义的存储过程来更新这样的EntitySet。当多对多关联在存储模型中所映射到的实体表示关系架构中的链接表时,必须为此链接表在AssociationSetMapping 元素中定义一个QueryView元素。定义查询视图时,不能在 AssociactionSetMapping 元素上指定 StorageSetName 属性。定义查询视图时,AssociationSetMapping 元素不能同时包含 EndProperty 映射。

你可能感兴趣的:(framework,entity)