實體資料模型 (EDM) 實體和關聯是應用程式程式碼中可程式化物件的正式規格。不管目前有沒有資料,實體和關聯都會宣告在概念結構定義語言 (CSDL) 語法中。在 CSDL 中撰寫結構描述通常是設計 EDM 上建置之應用程式的第一個步驟。
這裡及相關主題中所示範的 EDM 應用程式會使用 Northwind 範例資料庫中的現有資料,因此儲存模型已經實作。當您在應用程式定義域中定義實體類型和關聯時,使用這個應用程式中的資料需要特別注意現有資料表的結構。
此實作透過使用從結構描述和對應規格中建置的物件模型來包括概念規格、儲存中繼資料、對應規格和應用程式程式碼。
本章節的結構描述範例會定義四個實體和兩個關聯。定義的實體類型是衍生自與 ADO.NET 一起安裝的基底類別結構描述。如需結構描述和對應的概觀,請參閱結構描述和對應規格 (Entity Framework)。
在下列範例中,開頭標記 <Schema> 包含了所定義之命名空間的宣告:NorthwindLib。此命名空間有別名 Self,此別名是當做結構描述主體內的簡短識別項使用。開頭標記 <Schema> 中的 URL 在所有 EDM 應用程式中都是相同的。
<Documentation> 標記包含對開發人員有用的資訊。文件標記之間的文字不只是程式碼註解;當建置物件程式庫之後,資訊會出現在 Visual Studio 中的物件瀏覽器和用來瀏覽類別的其他工具內。在此範例中,文字可識別這個 XML 檔案中所定義的結構描述。
此範例中的三個實體是定義在 <EntityType> 標記內。每一個實體都包含一個 Key 屬性 (Attribute) 來指示實體的屬性 (Property),該實體是此型別之執行個體的唯一識別項。Name 和 End Role 屬性會指定兩個 <Association> 型別,以提供關聯上相關之型別的範圍。在此範例中,一個關聯和導覽屬性會將 Product 實體的執行個體連接到邏輯上相關的 Category 實體。另一個關聯會將 Order 執行個體連接到下訂單之 Customers 的執行個體。如需關聯的詳細資訊,請參閱 Entity Data Model 關聯性。
<?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 的屬性包含以下項目:ProductID、ProductName、UnitPrice 以及名為 Category 的 NavigationProperty (可將這個產品與一組類似產品產生關聯)。這一組屬性會定義 Product 實體。
EDM <EntityContainer> 標記會指定一個容器,此容器將會對應到儲存中繼資料結構描述中的資料庫物件 (dbo)。在這個 <EntityContainer> 中,有三個 <EntitySet> 規格和兩個 <AssociationSet> 規格。
如需這個 CSDL 檔案中使用之 XML 語法的詳細資訊,請參閱結構描述 (EDM)。如需導覽屬性的資訊,請參閱實作關聯 (EDM)。
另請參閱
概念
Entity Data Model 類型
Entity Data Model 關聯性
結構描述 (EDM)
實作關聯 (EDM)