共用方式為


Schema 項目 (CSDL)

Schema 項目是概念模型定義的根項目。其中包含組成概念模型的物件、函式和容器等定義。

Schema 項目可以包含零或下列多個子項目:

一個 Schema 項目可以包含零或多個 Annotation 項目

注意

Function 項目和註解項目僅能用於應用程式的概念模型,該應用程式的目標必須是 .NET Framework 4 或以後版本。這類模型的 XML 命名空間是 https://schemas.microsoft.com/ado/2008/09/edm。

Schema 項目使用 Namespace 屬性,定義概念模型中的實體類型命名空間、複雜型別和關聯物件。在命名空間中,兩個物件不能有相同的名稱。命名空間可以跨多個 Schema 項目和多個 .csdl 檔案。

概念模型命名空間與 Schema 項目的 XML 命名空間不同。概念模型命名空間 (由 Namespace 屬性定義) 是實體類型、複雜型別和關聯型別的邏輯容器。Schema 項目的 XML 命名空間 (由 xmlns 屬性表示) 是子項目和 Schema 項目屬性的預設命名空間。XML 命名空間的格式 https://schemas.microsoft.com/ado/YYYY/MM/edm (其中的 YYYY 和 MM 分別表示年和月) 是保留給 CSDL 之用。自訂項目和屬性不能出現在擁有此格式的命名空間中。

適用屬性

下表描述可套用至 Schema 項目的屬性。

屬性名稱

必要

Namespace

概念模型的命名空間。Namespace 屬性的值是用來構成型別的完整限定名稱。例如,如果在 Simple.Example.Model 命名空間中有名為 Customer的EntityType,則EntityType 的完整名稱是 SimpleExampleModel.Customer。

以下字串不能用來作為 Namespace 屬性的值:System、Transient 或 Edm。Namespace 屬性的值與 SSDL Schema 項目中的 Namespace 屬性值,兩者不能相同。

Alias

用來取代命名空間名稱的識別項。例如,如果在 Simple.Example.Model 命名空間中有一個名為 Customer 的 EntityType,且 Alias 屬性的值是 Model,您可以使用 Model.Customer 做為 EntityType.的完整名稱。

注意

任何數量的附註屬性 (自訂 XML 屬性) 都可以套用至 Schema 項目。不過,自訂屬性不可屬於任何 XML 命名空間,這是保留供 CSDL 使用。任兩個自訂屬性的完整名稱不能相同。

範例

下列範例顯示一個 Schema 項目,其中包含一個 EntityContainer 項目、兩個 EntityType 項目和一個 Association 項目。

<Schema xmlns="https://schemas.microsoft.com/ado/2008/09/edm" 
     xmlns:cg="https://schemas.microsoft.com/ado/2006/04/codegeneration"
     xmlns:store="https://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" 
      Namespace="ExampleModel" Alias="Self">
        <EntityContainer Name="ExampleModelContainer">
          <EntitySet Name="Customers" 
                     EntityType="ExampleModel.Customer" />
          <EntitySet Name="Orders" EntityType="ExampleModel.Order" />
          <AssociationSet 
                      Name="CustomerOrder" 
                      Association="ExampleModel.CustomerOrders">
            <End Role="Customer" EntitySet="Customers" />
            <End Role="Order" EntitySet="Orders" />
          </AssociationSet>
        </EntityContainer>
        <EntityType Name="Customer">
          <Key>
            <PropertyRef Name="CustomerId" />
          </Key>
          <Property Type="Int32" Name="CustomerId" Nullable="false" />
          <Property Type="String" Name="Name" Nullable="false" />
          <NavigationProperty 
                   Name="Orders" 
                   Relationship="ExampleModel.CustomerOrders" 
                   FromRole="Customer" ToRole="Order" />
        </EntityType>
        <EntityType Name="Order">
          <Key>
            <PropertyRef Name="OrderId" />
          </Key>
          <Property Type="Int32" Name="OrderId" Nullable="false" />
          <Property Type="Int32" Name="ProductId" Nullable="false" />
          <Property Type="Int32" Name="Quantity" Nullable="false" />
          <NavigationProperty 
                   Name="Customer" 
                   Relationship="ExampleModel.CustomerOrders" 
                   FromRole="Order" ToRole="Customer" />
          <Property Type="Int32" Name="CustomerId" Nullable="false" />
        </EntityType>
        <Association Name="CustomerOrders">
          <End Type="ExampleModel.Customer" 
               Role="Customer" Multiplicity="1" />
          <End Type="ExampleModel.Order" 
               Role="Order" Multiplicity="*" />
          <ReferentialConstraint>
            <Principal Role="Customer">
              <PropertyRef Name="CustomerId" />
            </Principal>
            <Dependent Role="Order">
              <PropertyRef Name="CustomerId" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
      </Schema>

請參閱

概念

Entity Framework 概觀

CSDL 規格

其他資源

CSDL、SSDL 和 MSL 規格

ADO.NET Entity Data Model Tools