Northwind 概念架构 (EDM)

实体数据模型 (EDM) 实体和关联是应用程序代码中可编程对象的正式规范。无论是否使用现有数据,实体和关联都是以概念性架构定义语言 (CSDL) 语法声明的。在 CSDL 中编写架构通常是设计在 EDM 上构建的应用程序的第一步。

本主题和相邻主题中演示的 EDM 应用程序使用 Northwind 示例数据库中的现有数据,因此已经实现了存储模型。在应用程序域定义实体类型和关联时,使用此应用程序中的数据需要注意现有表的结构。

通过使用从架构和映射规范生成的对象模型,该实现包括概念性规范、存储元数据、映射规范和应用程序代码。

本节中的架构示例定义了四个实体和两个关联。定义的实体类型是从 ADO.NET 中安装的基类架构派生的。有关架构和映射的概述,请参见架构和映射规范(实体框架)

在下面的示例中,<Schema> 开始标记包含定义 NorthwindLib 命名空间的声明。该命名空间的别名 Self 在架构主体中用作短标识符。在所有 EDM 应用程序中,<Schema> 开始标记中的 URL 都相同。

<Documentation> 标记包含对开发人员有用的信息。文档标记之间的文本不仅表示代码注释;在生成对象库之后,这些信息会显示在 Visual Studio 中的对象浏览器和用于浏览类的其他工具中。在此示例中,文本标识在此 XML 文件中定义的架构。

此示例中的三个实体在 <EntityType> 标记内定义。每个实体都包含一个 Key 属性 (Attribute),表示作为此类型实例的唯一标识符的实体属性 (Property)。两个 <Association> 类型由 NameEnd Role 属性指定,提供由关联相关的类型的作用域。在此示例中,一个关联和导航属性将 Product 实体的实例和与其逻辑相关的 Category 实体连接起来。另一个关联将 Order 实例与发出订单的 Customers 的实例连接起来。有关关联的更多信息,请参见实体数据模型关系

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

  <EntityType Name="Product">
    <Key>
      <PropertyRef Name="ProductID" />
    </Key>
    <Property Name="ProductID" Type="Int32" Nullable="false" />
    <Property Name="ProductName" Type="String" 
                Nullable="false" ConcurrencyMode="Fixed" />
    <Property Name="UnitPrice" Type="Decimal" 
                Nullable="true" ConcurrencyMode="Fixed" />
    <NavigationProperty Name="Category"
          Relationship="Self.Category_Product" FromRole="Product"
          ToRole="Category" />
  </EntityType>

  <EntityType Name="DiscontinuedProduct" BaseType="Self.Product">
    <!-- Units in stock for discontinued products. -->
    <Property Name="UnitsInStock" Type="Int16" Nullable="true" />
  </EntityType>

  <EntityType Name="Customer">
    <Key>
      <PropertyRef Name="CustomerID" />
    </Key>
    <Property Name="CustomerID" Type="String" Nullable="false" />
    <Property Name="CompanyName" Type="String"
              Nullable="false" ConcurrencyMode="Fixed" />
    <Property Name="ContactName" Type="String"
              Nullable="true" ConcurrencyMode="Fixed" />
    <Property Name="City" Type="String"
              Nullable="true" ConcurrencyMode="Fixed" />
    <Property Name="Country" Type="String"
              Nullable="true" ConcurrencyMode="Fixed" />
    <NavigationProperty Name="SalesOrders"
            Relationship="Self.Customer_Order" FromRole="Customer"
            ToRole="SalesOrder" />
  </EntityType>

  <EntityType Name="Category">
    <Key>
      <PropertyRef Name="CategoryID" />
    </Key>
    <Property Name="CategoryID" Type="Int32" Nullable="false" />
    <Property Name="CategoryName" Type="String"
                  Nullable="false" ConcurrencyMode="Fixed" />
    <Property Name="Description" Type="String" Nullable="true" />
    <NavigationProperty Name="Products"
        Relationship="Self.Category_Product"
        FromRole="Category" ToRole="Product" />
  </EntityType>

  <EntityType Name="SalesOrder">
    <Key>
      <PropertyRef Name="OrderID" />
    </Key>
    <Property Name="OrderID" Type="Int32" Nullable="false" />
    <Property Name="OrderDate" Type="DateTime" Nullable="true" />
    <Property Name="ShipCity" Type="String" Nullable="true" />
    <Property Name="ShipCountry" Type="String" Nullable="true" />
    <NavigationProperty Name="Customer"
       Relationship="Self.Customer_Order"
       FromRole="SalesOrder" ToRole="Customer" />
  </EntityType>

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

  <Association Name="Category_Product">
    <End Role="Category" Type="Self.Category" Multiplicity="1" />
    <End Role="Product" Type="Self.Product" Multiplicity="*" />
  </Association>

  <EntityContainer Name="Northwind">
    <EntitySet Name="Categories" EntityType="Self.Category" />
    <EntitySet Name="Products" EntityType="Self.Product" />
    <EntitySet Name="Customers" EntityType="Self.Customer" />
    <EntitySet Name="SalesOrders" EntityType="Self.SalesOrder" />
    <AssociationSet Name="CustomerOrders"
                  Association="Self.Customer_Order">
      <End Role="Customer" EntitySet="Customers" />
      <End Role="SalesOrder" EntitySet="SalesOrders" />
    </AssociationSet>
    <AssociationSet Name="CategoryProducts"
                  Association="Self.Category_Product">
      <End Role="Category" EntitySet="Categories" />
      <End Role="Product" EntitySet="Products" />
    </AssociationSet>
  </EntityContainer>

</Schema>

此架构中使用最频繁的标记是 <Property> 标记。属性指定每个 <EntityType> 所包含的各种数据。例如,Product 的属性包括:ProductIDProductNameUnitPrice 以及名为 Category(将产品与一组类似产品关联)的 NavigationProperty。这组属性定义 Product 实体。

EDM <EntityContainer> 标记指定一个容器,该容器将映射到存储元数据架构中的数据库对象 (dbo)。在此 <EntityContainer> 中,有三个 <EntitySet> 规范和两个 <AssociationSet> 规范。

有关该 CSDL 文件中使用的 XML 语法的更多信息,请参见架构 (EDM)。有关导航属性的信息,请参见实现关联 (EDM)

另请参见

概念

实体数据模型类型
实体数据模型关系
架构 (EDM)
实现关联 (EDM)

其他资源

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