架构 (EDM)

实体数据模型 (EDM) 架构是用于描述实体类型和关系类型的 XML 文本文件。架构还定义一个容器,在此容器中,将在逻辑上组织这些类型的实例。EDM 中的实体和关联同时定义随 实体框架 安装的基类型和开发人员设计的类型。

设计架构以概念架构定义语言 (CSDL) 编写。以存储架构定义语言 (SSDL) 编写的第二个架构为使用 EDM 的应用程序定义存储模型。

本主题描述以 CSDL 编写的设计架构。有关存储元数据和映射架构的更多信息,请参见架构和映射规范(实体框架)

架构中的顶级元素指定将在其中应用名称唯一性规则的命名空间。将在命名空间 MyCompany.LOBSchema 之下,从逻辑上对在以下简化架构中定义的类型进行组织。此命名空间类似于面向对象的编程中包含类和过程代码的命名空间。EDM 不定义过程。有关在 EDM 中实现过程的信息,请参见帮助器方法 (EDM)

在以下架构中定义的每个实体和关联都具有完全限定名称,如 MyCompany.LOBSchema.Customer。在本示例中,通过为命名空间 MyCompany.LOBSchema 声明的 Self 别名(如使用 Self.Customer)缩短了完全限定名称。

实体和关联在架构的主体中定义。在本示例中定义的实体的属性已被忽略,以显示 CSDL 架构的总体结构。

<?xml version="1.0" encoding="utf-8"?>
<Schema xmlns="https://schemas.microsoft.com/ado/2006/04/edm" 
        Namespace="MyCompany.LOBSchema" Alias="Self">

    <Documentation>
      <Summary>Conceptual schema (csdl)</Summary>
      <LongDescription>This schema is an example showing 
        general syntax.</LongDescription>
    </Documentation>

    <EntityType Name="Customer">
        <Key>
            <PropertyRef Name="CustomerId" />
        </Key>
        <Property Name="CustomerId" Type="Guid" Nullable="false" />
        <!-- Other properties-->
    </EntityType>

    <EntityType Name="Order">
        <Key>
            <PropertyRef Name="OrderId" />
        </Key>
        <Property Name="OrderId" Type="Guid" Nullable="false" />
        <!-- Other properties-->
    </EntityType>

    <Association Name="Customer_Order">
        <End Role="Customer" Type="Self.Customer" Multiplicity="0..1" />
        <End Role="Order" Type="Self.Order" Multiplicity="*" />
    </Association>

    <EntityContainer Name="LOBSchemaData">
        <EntitySet Name="Customers" EntityType="Self.Customer" />
        <EntitySet Name="Orders" EntityType="Self.Order" />
        <AssociationSet Name="Customer_Orders" Association="Self.Customer_Order">
            <End Role="Customer" EntitySet="Customers" />
            <End Role="Order" EntitySet="Orders" />
        </AssociationSet>
    </EntityContainer>
</Schema>

可以使用 Using 标记定义在其他 CSDL 文件中定义的架构的别名,这样,就可以在未使用完全限定语法定义的命名空间中使用外部架构中的现有类型。

在完整的架构中,实体声明包含属性。其中一个属性被指定为 Key 属性。EntityTypeKey 属性 (Attribute) 将实体的一个或多个属性 (Property) 指定为 KeyKey 可以是多个属性的组合。Key 针对所有操作唯一地标识某个实体的实例。

下面示例的开始标记EntityType 中,在为实体指定 Name 属性之后,为 Key 属性指定 CustomerIdCustomerIdKey 属性 (Property) 的 Type 属性 (Attribute) 设置为 Guid,以便为 Key 指定唯一值。CustomerId 属性的声明紧随在此 CSDL 段的第二行中。

    <EntityType Name="Customer" >
        <Key>
            <PropertyRef Name="CustomerId" />
        </Key>
        <Property Name="CustomerId" Type="Guid" Nullable="false" />
        <Property Name="Title" Type="String" Nullable="false" />
    </EntityType>

本示例的最后一行声明名为 Title 的属性并将 Title 属性的 Type 指定为 StringTitle 属性标识客户的工作角色。Title 属性不能为 Null,如约束 Nullability="false" 所指定。Title 属性 (Property) 的 String 类型的 Default 属性 (Attribute) 设置为 "NewHire"

可以按以下示例中所示指定组合键。

<EntityType Name="EmployeePayHistory">
    <Key>
      <PropertyRef Name="EmployeeID" />
      <PropertyRef Name="RateChangeDate" />
    </Key>
    <Property Name="EmployeeID" Type="Int32" Nullable="false" />
    <Property Name="RateChangeDate" Type="DateTime" Nullable="false"/>
    <Property Name="Rate" Type="Decimal" Nullable="false"/>
    <Property Name="PayFrequency" Type="Boolean" Nullable="false" />
    <Property Name="ModifiedDate" Type="DateTime" Nullable="false" />
    <NavigationProperty Name="Employee"
Relationship="Adventureworks.FK_EmployeePayHistory_Employee_EmployeeID"
 FromRole="EmployeePayHistory" ToRole="Employee" />
  </EntityType>

实体容器

使用 EDM 的架构定义了一个实体容器,以指定可用于正在定义的命名空间中的数据类型集。对于将从 CSDL 架构生成的可编程对象模型,实体容器是其中的命名空间的基础。

在 XML 层次结构中,EntityContainer 元素独立于 Schema 元素,即使 EntityContainer 是在架构中定义的也是如此。这在将 EntityContainer 映射到存储时十分重要。概念架构中的EntityContainer 标记映射到存储元数据中对应的 EntityContainer 标记。请注意,在映射文件中,EntityContainer 的完全限定名称不包含架构命名空间名称。

实体集对可编程对象模型的类建模。关联集定义关系的作用域。有关实体集和关联集的更多信息,请参见实体集 (EDM)关联集 (EDM)

下面的架构段包含 MyCompany.LOBSchema 命名空间中的 LOBSchemaData 命名空间的EntityContainer 声明。

    <EntityContainer Name="LOBSchemaData">
        <EntitySet Name="Customers" EntityType="Self.Customer"/>
        <EntitySet Name="Orders" EntityType="Self.Order"/>
        <AssociationSet Name="Customer_Orders" 
                          Association="Self.Customer_Order">
            <End Role="Customer" EntitySet="Customers"/>
            <End Role="Order" EntitySet="Orders"/>
        </AssociationSet>
    </EntityContainer>

EntityContainer 声明包含之前代码段中定义的所有实体和关联,但它们现在的形式是由 EntitySetAssociationSet 标记指定的集。

将此架构的规范实现为数据类型时,CustomerOrder 的每个实例都将成为相应 EntitySet 的成员。此实体容器定义 CustomerOrder 实体的实体集,这些实体集以复数形式命名:CustomersOrders。开发人员可以定义自己的命名约定。有关这些名称如何与存储元数据中的名称相对应以及映射规范的更多信息,请参见架构和映射规范(实体框架)

前面的 EntityContainer 声明中的 AssociationSet 标记指定一个名为 Customer_Orders 的关联集。此关联集包含 Customer_Order 关联的实例。

这些元素(EntityTypeEntitySetAssociationAssociationSetEntityContainer)组合起来形成了架构的基础结构。有关架构中的声明的更多逐行信息,请参见架构和映射规范(实体框架)

部署

EDM 规范并未指定在 CSDL 中定义实体类型之后所使用的存储模型。在大多数常见方案中,数据源是关系数据库管理系统 (RDBMS)。部署存储模型时,要求创建数据库以及与在架构中定义的实体相对应的表。实现可能还要求数据表和/或链接表中的外键以支持关联。

通过将数据库和表映射到 EDM 类型,可以将 EDM 用于现有数据库。现有编程对象模型也可以映射到 EDM 类型。

另请参见

概念

实体数据模型关系
实体数据模型类型
EntityType 元素 (CSDL)
EntitySet 元素 (EntityContainer CSDL)
Association 元素 (CSDL)
AssociationSet 元素 (EntityContainer CSDL)
EntityContainer 元素 (CSDL)

其他资源

架构和映射规范(实体框架)