SSDL 規格

存放結構定義語言 (SSDL) 是一種 XML 架構語言,會描述 Entity Framework 應用程式的儲存體模型。

在 Entity Framework 應用程式中,儲存體模型中繼資料會從 .ssdl 檔案(以 SSDL 撰寫)載入 System.Data.Metadata.Edm.StoreItemCollection 的實例,而且可以使用 System.Data.Metadata.Edm.MetadataWorkspace 類別中的方法來存取。 Entity Framework 會使用儲存體模型中繼資料,將查詢轉譯至概念模型,以儲存特定的命令。

Entity Framework Designer (EF Designer) 會在設計階段將儲存體模型資訊儲存在 .edmx 檔案中。 在建置階段,實體設計工具會使用 .edmx 檔案中的資訊,在執行時間建立 Entity Framework 所需的 .ssdl 檔案。

SSDL 的版本可藉由 XML 命名空間來區別。

SSDL 版本 XML 命名空間
SSDL v1 https://schemas.microsoft.com/ado/2006/04/edm/ssdl
SSDL v2 https://schemas.microsoft.com/ado/2009/02/edm/ssdl
SSDL v3 https://schemas.microsoft.com/ado/2009/11/edm/ssdl

Association 項目 (SSDL)

存放區架構定義語言 (SSDL) 中的 Association 元素會指定參與基礎資料庫中外鍵條件約束的資料表資料行。 兩個必要的子 End 元素會指定關聯結尾的資料表,以及每個結尾的多重性。 選擇性的 ReferentialConstraint 元素會指定關聯主體和相依端,以及參與的資料行。 如果沒有 ReferentialConstraint 元素存在,則必須使用 AssociationSetMapping 元素來指定關聯的資料行對應。

Association 元素可以有下列子項目(依列出的順序):

  • 檔 (零或一個)
  • 結束 (正好兩個)
  • ReferentialConstraint (零或一個)
  • 注釋專案 (零個或多個)

適用屬性

下表描述可套用至 Association 元素的屬性。

屬性名稱 是必要的
名稱 Yes 基礎資料庫中對應之外部索引鍵條件約束的名稱。

注意

任何數目的注釋屬性(自訂 XML 屬性)都可以套用至 Association 元素。 不過,自訂屬性不可屬於任何 XML 命名空間,這是保留供 SSDL 使用。 任兩個自訂屬性的完整名稱不能相同。

範例

下列範例顯示 使用 ReferentialConstraint 元素來指定參與FK_CustomerOrders 外鍵條件約束的資料行的 Association 元素:

 <Association Name="FK_CustomerOrders">
   <End Role="Customers"
        Type="ExampleModel.Store.Customers" Multiplicity="1">
     <OnDelete Action="Cascade" />
   </End>
   <End Role="Orders"
        Type="ExampleModel.Store.Orders" Multiplicity="*" />
   <ReferentialConstraint>
     <Principal Role="Customers">
       <PropertyRef Name="CustomerId" />
     </Principal>
     <Dependent Role="Orders">
       <PropertyRef Name="CustomerId" />
     </Dependent>
   </ReferentialConstraint>
 </Association>

AssociationSet 項目 (SSDL)

存放區架構定義語言 (SSDL) 中的 AssociationSet 元素代表基礎資料庫中兩個數據表之間的外鍵條件約束。 參與外鍵條件約束的資料表資料行是在 Association 元素中指定。 對應至指定 AssociationSet 專案的 Association 元素是在 AssociationSet 專案的 Association 屬性 指定。

SSDL 關聯集會由 AssociationSetMapping 元素對應至 CSDL 關聯集。 不過,如果使用 ReferentialConstraint 元素定義給定 CSDL 關聯集的 CSDL 關聯,則不需要對應的 AssociationSetMapping 元素。 在此情況下,如果 AssociationSetMapping 元素存在,則會由 ReferentialConstraint 元素覆寫它所定義的對應。

AssociationSet 元素可以有下列子項目(依列出的順序):

  • 檔 (零或一個)
  • End (零或兩個)
  • 注釋專案 (零個或多個)

適用屬性

下表描述可套用至 AssociationSet 元素的屬性。

屬性名稱 是必要的
名稱 Yes 關聯集所表示之外部索引鍵條件約束的名稱。
協會 Yes 定義參與外部索引鍵條件約束之資料行的關聯名稱。

注意

任何數目的注釋屬性(自訂 XML 屬性)都可以套用至 AssociationSet 元素。 不過,自訂屬性不可屬於任何 XML 命名空間,這是保留供 SSDL 使用。 任兩個自訂屬性的完整名稱不能相同。

範例

下列範例顯示 AssociationSet 元素,表示基礎資料庫中的 FK_CustomerOrders 外鍵條件約束:

 <AssociationSet Name="FK_CustomerOrders"
                 Association="ExampleModel.Store.FK_CustomerOrders">
   <End Role="Customers" EntitySet="Customers" />
   <End Role="Orders" EntitySet="Orders" />
 </AssociationSet>

CollectionType 元素 (SSDL)

存放區架構定義語言 (SSDL) 中的 CollectionType 元素會指定函式的傳回類型是集合。 CollectionType 元素是 ReturnType 專案的子系。 集合的類型是使用 RowType 子專案來指定:

注意

任何數目的注釋屬性(自訂 XML 屬性)都可以套用至 CollectionType 元素。 不過,自訂屬性不可屬於任何 XML 命名空間,這是保留供 SSDL 使用。 任兩個自訂屬性的完整名稱不能相同。

範例

下列範例顯示使用 CollectionType 元素指定函式傳回資料列集合的函式。

   <Function Name="GetProducts" IsComposable="true" Schema="dbo">
     <ReturnType>
       <CollectionType>
         <RowType>
           <Property Name="ProductID" Type="int" Nullable="false" />
           <Property Name="CategoryID" Type="bigint" Nullable="false" />
           <Property Name="ProductName" Type="nvarchar" MaxLength="40" Nullable="false" />
           <Property Name="UnitPrice" Type="money" />
           <Property Name="Discontinued" Type="bit" />
         </RowType>
       </CollectionType>
     </ReturnType>
   </Function>

CommandText 項目 (SSDL)

存放區架構定義語言 (SSDL) 中的 CommandText 元素是 Function 元素的子系,可讓您定義在資料庫上執行的 SQL 語句。 CommandText 元素可讓您新增與資料庫中預存程式類似的功能,但您可以在儲存模型中定義 CommandText 元素。

CommandText 元素不能有子專案。 CommandText 元素的 主體必須是基礎資料庫的有效 SQL 語句。

沒有任何屬性適用于 CommandText 元素。

範例

下列範例顯示 具有子 CommandText 元素的 Function 元素。 藉 由將 UpdateProductInOrder 函式匯入至概念模型,將 UpdateProductInOrder 函式公開為 ObjectCoNtext 上的方法。  

 <Function Name="UpdateProductInOrder" IsComposable="false">
   <CommandText>
     UPDATE Orders
     SET ProductId = @productId
     WHERE OrderId = @orderId;
   </CommandText>
   <Parameter Name="productId"
              Mode="In"
              Type="int"/>
   <Parameter Name="orderId"
              Mode="In"
              Type="int"/>
 </Function>

DefiningQuery 項目 (SSDL)

存放區架構定義語言 (SSDL) 中的 DefiningQuery 元素可讓您直接在基礎資料庫中執行 SQL 語句。 DefiningQuery 元素通常會像資料庫檢視一樣使用,但檢視是在儲存體模型中定義,而不是資料庫。 DefiningQuery 元素中 定義的檢視可以透過 EntitySetMapping 元素對應至概念模型中的實體類型。 這些對應是唯讀的。  

下列 SSDL 語法顯示 EntitySet 宣告, 後面接著包含用來擷取檢視之查詢的 DefiningQuery 元素。

 <Schema>
     <EntitySet Name="Tables" EntityType="Self.STable">
         <DefiningQuery>
           SELECT  TABLE_CATALOG,
                   'test' as TABLE_SCHEMA,
                   TABLE_NAME
           FROM    INFORMATION_SCHEMA.TABLES
         </DefiningQuery>
     </EntitySet>
 </Schema>

您可以使用 Entity Framework 中的預存程式來啟用檢視的讀寫案例。 您可以使用資料來源檢視或 Entity SQL 檢視做為擷取資料的基表,以及預存程式進行變更處理。

您可以使用 DefiningQuery 元素以 Microsoft SQL Server Compact 3.5 為目標。 雖然 SQL Server Compact 3.5 不支援預存程式,但您可以使用 DefiningQuery 元素實作類似的功能 。 該項目的另一個用處是可建立預存程序,克服程式設計語言中所使用之資料類型與資料來源之資料類型間不相符的問題。 您可以撰寫 DefiningQuery 來接受一組特定參數,然後呼叫具有不同參數集的預存程式,例如,刪除資料的預存程式。

Dependent 項目 (SSDL)

存放區架構定義語言中的 Dependent 元素 (SSDL) 是 ReferentialConstraint 元素的子項目,可定義外鍵條件約束的相依結尾(也稱為引用條件約束)。 Dependent 元素會指定資料表中參考主鍵資料行(或資料行)的資料行(或資料行)。 PropertyRef 元素會指定參考哪些資料行。 Principal 元素會指定 Dependent 元素中所 指定資料行所參考的主鍵資料行。

Dependent 元素可以有下列子項目(依所列的順序):

  • PropertyRef (一或多個)
  • 注釋專案 (零個或多個)

適用屬性

下表描述可套用至 Dependent 元素的屬性。

屬性名稱 是必要的
角色 Yes 對應 End 元素的 Role 屬性相同值,否則為包含參考資料行的資料表名稱。

注意

任何數目的注釋屬性(自訂 XML 屬性)都可以套用至 Dependent 元素。 不過,自訂屬性不可屬於任何 XML 命名空間,這是保留給 CSDL 之用。 任兩個自訂屬性的完整名稱不能相同。

範例

下列範例顯示使用 ReferentialConstraint 元素來指定參與FK_CustomerOrders 外鍵條件約束的資料行的 Association 元素。 Dependent 元素會將 Order 資料表的 CustomerId 資料行指定為條件約束的相依結尾。

 <Association Name="FK_CustomerOrders">
   <End Role="Customers"
        Type="ExampleModel.Store.Customers" Multiplicity="1">
     <OnDelete Action="Cascade" />
   </End>
   <End Role="Orders"
        Type="ExampleModel.Store.Orders" Multiplicity="*" />
   <ReferentialConstraint>
     <Principal Role="Customers">
       <PropertyRef Name="CustomerId" />
     </Principal>
     <Dependent Role="Orders">
       <PropertyRef Name="CustomerId" />
     </Dependent>
   </ReferentialConstraint>
 </Association>

Documentation 項目 (SSDL)

存放區架構定義語言 (SSDL) 中的 Documentation 元素可用來提供父元素中定義之物件的相關資訊。

Documentation 元素可以有下列子項目(依所列順序):

  • 摘要 :父元素的簡短描述。 (零或一個項目)
  • LongDescription :父元素的廣泛描述。 (零或一個項目)

適用屬性

任何數目的注釋屬性(自訂 XML 屬性)都可以套用至 Documentation 元素。 不過,自訂屬性不可屬於任何 XML 命名空間,這是保留給 CSDL 之用。 任兩個自訂屬性的完整名稱不能相同。

範例

下列範例顯示 Documentation 元素做為 EntityType 元素的子專案。

 <EntityType Name="Customers">
   <Documentation>
     <Summary>Summary here.</Summary>
     <LongDescription>Long description here.</LongDescription>
   </Documentation>
   <Key>
     <PropertyRef Name="CustomerId" />
   </Key>
   <Property Name="CustomerId" Type="int" Nullable="false" />
   <Property Name="Name" Type="nvarchar(max)" Nullable="false" />
 </EntityType>

End 項目 (SSDL)

存放區架構定義語言 (SSDL) 中的 End 元素會在基礎資料庫中外鍵條件約束的一端指定資料表和資料列數目。 End 元素可以是 Association 元素或 AssociationSet 專案的子系。 在這兩種情況下,可能的子項目和適用的屬性都不相同。

End 項目為 Association 項目的子項目

End 元素(做為 Association 元素的 子系)會分別指定外鍵條件約束結尾的資料表和資料列數目,以及 Type Multiplicity 屬性。 外部索引鍵條件約束的端點是定義為 SSDL 關聯的一部分;SSDL 關聯必須擁有兩個端點。

End 元素可以有下列子項目(依所列的順序):

  • 檔 (零或一個專案)
  • OnDelete (零或一個專案)
  • 注釋專案 (零個或多個元素)

適用屬性

下表描述當它是 Association 元素的子系時,可以套用至 End 元素的屬性

屬性名稱 是必要的
類型 Yes 位於外鍵條件約束結尾之 SSDL 實體集的完整名稱。
角色 No 對應 ReferentialConstraint 元素之 Principal 或 Dependent 元素中 Role 屬性的值 (如果使用的話)。
多重性 Yes 1 0..1 * 取決於位於外鍵條件約束結尾的資料列數目。
1 表示外鍵條件約束結尾只存在一個資料列。
0..1 表示外鍵條件約束結尾存在零或一個資料列。
* 表示外鍵條件約束結尾存在零、一或多個資料列。

注意

任何數目的注釋屬性(自訂 XML 屬性)都可以套用至 End 元素。 不過,自訂屬性不可屬於任何 XML 命名空間,這是保留給 CSDL 之用。 任兩個自訂屬性的完整名稱不能相同。

範例

下列範例顯示 定義FK_CustomerOrders 外鍵條件約束的 Association 元素。 在每個 End 元素上指定的多重性 值表示 Orders 資料表中的 許多資料列可以與 Customers 資料表中的資料 列相關聯,但 Customers 資料表中 只有一個資料列可以與 Orders 資料表中的資料 列相關聯。 此外,OnDelete 元素表示如果 Customers 資料表中的資料 列已刪除,則會刪除 Orders 資料表中 參考客戶 資料表中特定資料列的所有資料列

 <Association Name="FK_CustomerOrders">
   <End Role="Customers"
        Type="ExampleModel.Store.Customers" Multiplicity="1">
     <OnDelete Action="Cascade" />
   </End>
   <End Role="Orders"
        Type="ExampleModel.Store.Orders" Multiplicity="*" />
   <ReferentialConstraint>
     <Principal Role="Customers">
       <PropertyRef Name="CustomerId" />
     </Principal>
     <Dependent Role="Orders">
       <PropertyRef Name="CustomerId" />
     </Dependent>
   </ReferentialConstraint>
 </Association>

End 項目為 AssociationSet 項目的子項目

End 元素 (作為 AssociationSet 元素的 子系)會在基礎資料庫中外鍵條件約束的一端指定資料表。

End 元素可以有下列子項目(依所列的順序):

  • 檔 (零或一個)
  • 注釋專案 (零個或多個)

適用屬性

下表描述當它是 AssociationSet 元素的子系時,可以套用至 End 元素的屬性

屬性名稱 是必要的
EntitySet Yes 位於外鍵條件約束結尾的 SSDL 實體集名稱。
角色 No 對應 Association 元素之一 End 元素上指定之 Role 屬性的值

注意

任何數目的注釋屬性(自訂 XML 屬性)都可以套用至 End 元素。 不過,自訂屬性不可屬於任何 XML 命名空間,這是保留給 CSDL 之用。 任兩個自訂屬性的完整名稱不能相同。

範例

下列範例顯示 EntityContainer 元素,其中具有具有兩 End 元素的 AssociationSet 元素:

 <EntityContainer Name="ExampleModelStoreContainer">
   <EntitySet Name="Customers"
              EntityType="ExampleModel.Store.Customers"
              Schema="dbo" />
   <EntitySet Name="Orders"
              EntityType="ExampleModel.Store.Orders"
              Schema="dbo" />
   <AssociationSet Name="FK_CustomerOrders"
                   Association="ExampleModel.Store.FK_CustomerOrders">
     <End Role="Customers" EntitySet="Customers" />
     <End Role="Orders" EntitySet="Orders" />
   </AssociationSet>
 </EntityContainer>

EntityContainer 項目 (SSDL)

存放區架構定義語言中的 EntityContainer 元素 (SSDL) 描述 Entity Framework 應用程式中基礎資料來源的結構:SSDL 實體集(定義于 EntitySet 元素中)代表資料庫中的資料表、SSDL 實體類型(定義于 EntityType 元素中)代表資料表中的資料列,而關聯集(在 AssociationSet 元素中定義)則代表資料庫中的外鍵條件約束。 儲存體模型實體容器會透過 EntityContainerMapping 元素對應至概念模型實體容器。

EntityContainer 元素可以有零或一個 Documentation 元素。 如果檔 元素存在,它必須位於所有其他子項目之前。

EntityContainer 元素可以有下列子項目的零或更多專案(依所列順序):

  • EntitySet
  • AssociationSet
  • Annotation 項目

適用屬性

下表描述可套用至 EntityContainer 元素的屬性。

屬性名稱 是必要的
名稱 Yes 實體容器的名稱。 此名稱不得包含任何句號 (.)。

注意

任何數目的注釋屬性(自訂 XML 屬性)都可以套用至 EntityContainer 元素。 不過,自訂屬性不可屬於任何 XML 命名空間,這是保留供 SSDL 使用。 任兩個自訂屬性的完整名稱不能相同。

範例

下列範例顯示定義兩個 實體集和一個關聯集的 EntityContainer 元素。 請注意實體類型和關聯類型名稱要以概念模型命名空間名稱限定。

 <EntityContainer Name="ExampleModelStoreContainer">
   <EntitySet Name="Customers"
              EntityType="ExampleModel.Store.Customers"
              Schema="dbo" />
   <EntitySet Name="Orders"
              EntityType="ExampleModel.Store.Orders"
              Schema="dbo" />
   <AssociationSet Name="FK_CustomerOrders"
                   Association="ExampleModel.Store.FK_CustomerOrders">
     <End Role="Customers" EntitySet="Customers" />
     <End Role="Orders" EntitySet="Orders" />
   </AssociationSet>
 </EntityContainer>

EntitySet 項目 (SSDL)

存放區架構定義語言 (SSDL) 中的 EntitySet 元素代表基礎資料庫中的資料表或檢視表。 SSDL 中的 EntityType 元素代表資料表或檢視中的資料列。 EntitySet 元素的 EntityType 屬性會指定代表 SSDL 實體集中資料列的特定 SSDL 實體類型。 CSDL 實體集與 SSDL 實體集之間的對應是在 EntitySetMapping 元素中指定。

EntitySet 元素可以有下列子項目(依所列的順序):

  • 檔 (零或一個專案)
  • DefiningQuery (零或一個專案)
  • Annotation 項目

適用屬性

下表描述可套用至 EntitySet 元素的屬性。

注意

某些屬性(此處未列出)可能符合 集別名。 更新模型精靈會在更新模型時使用這些屬性。

屬性名稱 是必要的
名稱 Yes 實體集的名稱。
EntityType Yes 實體類型 (實體集包含其執行個體) 的完整名稱。
結構描述 No 資料庫結構描述。
Table No 資料庫資料表。

注意

任何數目的注釋屬性(自訂 XML 屬性)都可以套用至 EntitySet 元素。 不過,自訂屬性不可屬於任何 XML 命名空間,這是保留供 SSDL 使用。 任兩個自訂屬性的完整名稱不能相同。

範例

下列範例顯示具有兩個 EntitySet 元素和一個 AssociationSet 元素的 EntityContainer 元素:

 <EntityContainer Name="ExampleModelStoreContainer">
   <EntitySet Name="Customers"
              EntityType="ExampleModel.Store.Customers"
              Schema="dbo" />
   <EntitySet Name="Orders"
              EntityType="ExampleModel.Store.Orders"
              Schema="dbo" />
   <AssociationSet Name="FK_CustomerOrders"
                   Association="ExampleModel.Store.FK_CustomerOrders">
     <End Role="Customers" EntitySet="Customers" />
     <End Role="Orders" EntitySet="Orders" />
   </AssociationSet>
 </EntityContainer>

EntityType 項目 (SSDL)

存放區架構定義語言 (SSDL) 中的 EntityType 元素代表基礎資料庫的資料表或檢視表中的資料列。 SSDL 中的 EntitySet 元素代表發生資料列的資料表或檢視表。 EntitySet 元素的 EntityType 屬性會指定代表 SSDL 實體集中資料列的特定 SSDL 實體類型。 SSDL 實體類型和 CSDL 實體類型之間的對應是在 EntityTypeMapping 元素中指定。

EntityType 元素可以有下列子項目(依所列的順序):

  • 檔 (零或一個專案)
  • 索引鍵 (零或一個專案)
  • Annotation 項目

適用屬性

下表描述可套用至 EntityType 元素的屬性。

屬性名稱 是必要的
名稱 Yes 實體類型的名稱。 此值通常與資料表名稱相同,在資料表中實體類型表示資料列。 此值不可以包含句號 (.)。

注意

任何數目的注釋屬性(自訂 XML 屬性)都可以套用至 EntityType 元素。 不過,自訂屬性不可屬於任何 XML 命名空間,這是保留供 SSDL 使用。 任兩個自訂屬性的完整名稱不能相同。

範例

下列範例顯示具有兩個 屬性的 EntityType 元素:

 <EntityType Name="Customers">
   <Documentation>
     <Summary>Summary here.</Summary>
     <LongDescription>Long description here.</LongDescription>
   </Documentation>
   <Key>
     <PropertyRef Name="CustomerId" />
   </Key>
   <Property Name="CustomerId" Type="int" Nullable="false" />
   <Property Name="Name" Type="nvarchar(max)" Nullable="false" />
 </EntityType>

Function 屬性 (SSDL)

存放區架構定義語言 (SSDL) 中的 Function 元素會指定存在於基礎資料庫中的預存程式。

Function 元素可以有下列子項目(依所列的順序):

  • 檔 (零或一個)
  • 參數 (零個或多個)
  • CommandText (零或一個)
  • ReturnType (零個或多個)
  • 注釋專案 (零個或多個)

函式的傳回型別必須使用 ReturnType 元素或 ReturnType 屬性來指定(請參閱下方),但不能同時指定 兩者。

儲存體模型中所指定的預存程序可以匯入應用程式的概念模型中。 如需詳細資訊,請參閱 使用預存程式 進行查詢。 Function 元素也可以用來在儲存模型中定義自訂函式。  

適用屬性

下表描述可套用至 Function 元素的屬性。

注意

某些屬性(此處未列出)可能符合 集別名。 更新模型精靈會在更新模型時使用這些屬性。

屬性名稱 是必要的
名稱 Yes 預存程序的名稱。
ReturnType No 預存程序的傳回型別。
彙總 No 如果預存程式傳回匯總值,則為 True ,否則 為 False
內置 No 如果函式是內 建的 1 函式,則為 True ,否則 為 False
StoreFunctionName No 預存程序的名稱。
NiladicFunction No 如果函式是 niladic 2 函式 ,則為 True ; 否則為 False
IsComposable No 如果函式是可 組合的 3 函式,則為 True ; 否則為 False
ParameterTypeSemantics No 列舉型別,可定義用來解決函式多載的型別語意。 列舉型別會定義在每個函式定義的提供者資訊清單。 預設值為 AllowImplicitConversion
結構描述 No 結構描述的名稱,可在其中定義預存程序。

1 內建函式是資料庫中定義的函式。 如需儲存模型中定義之函式的相關資訊,請參閱 CommandText 元素 (SSDL)。

2 niladic 函式是不接受任何參數的函式,呼叫時不需要括弧。

3 如果一個函式的輸出可以是另一個函式的輸入,則兩個函式是可組合的。

注意

任何數目的注釋屬性(自訂 XML 屬性)都可以套用至 Function 元素。 不過,自訂屬性不可屬於任何 XML 命名空間,這是保留供 SSDL 使用。 任兩個自訂屬性的完整名稱不能相同。

範例

下列範例顯示 對應至 UpdateOrderQuantity 預存程式的 Function 元素。 預存程序可接受兩個參數且不傳回值。

 <Function Name="UpdateOrderQuantity"
           Aggregate="false"
           BuiltIn="false"
           NiladicFunction="false"
           IsComposable="false"
           ParameterTypeSemantics="AllowImplicitConversion"
           Schema="dbo">
   <Parameter Name="orderId" Type="int" Mode="In" />
   <Parameter Name="newQuantity" Type="int" Mode="In" />
 </Function>

Key 項目 (SSDL)

存放區架構定義語言中的 Key 元素 (SSDL) 代表基礎資料庫中資料表的主鍵。 Key 是 EntityType 元素的子專案,代表資料表中的資料列。 主鍵是在 Key 元素中 定義,方法是參考 EntityType 元素上 定義的一或多個 Property 元素。

Key 元素可以有下列子項目(依所列的順序):

  • PropertyRef (一或多個)
  • Annotation 項目

沒有屬性適用于 Key 元素。

範例

下列範例顯示 具有參考一個屬性之索引鍵的 EntityType 元素:

 <EntityType Name="Customers">
   <Documentation>
     <Summary>Summary here.</Summary>
     <LongDescription>Long description here.</LongDescription>
   </Documentation>
   <Key>
     <PropertyRef Name="CustomerId" />
   </Key>
   <Property Name="CustomerId" Type="int" Nullable="false" />
   <Property Name="Name" Type="nvarchar(max)" Nullable="false" />
 </EntityType>

OnDelete 項目 (SSDL)

儲存架構定義語言 (SSDL) 中的 OnDelete 元素會在刪除參與外鍵條件約束的資料列時反映資料庫行為。 如果動作設定為 Cascade ,則會刪除參考所刪除資料列的資料列。 如果動作設定為 [無 ],則不會同時刪除參考所刪除之資料列的資料列。 OnDelete 元素是 End 元素的子專案。

OnDelete 元素可以有下列子項目(依所列的順序):

  • 檔 (零或一個)
  • 注釋專案 (零個或多個)

適用屬性

下表描述可套用至 OnDelete 元素的屬性。

屬性名稱 是必要的
動作 Yes 串聯 。 (值 Restricted 有效,但行為與 None 相同。

注意

任何數目的注釋屬性(自訂 XML 屬性)都可以套用至 OnDelete 元素。 不過,自訂屬性不可屬於任何 XML 命名空間,這是保留供 SSDL 使用。 任兩個自訂屬性的完整名稱不能相同。

範例

下列範例顯示 定義FK_CustomerOrders 外鍵條件約束的 Association 元素。 OnDelete 元素表示如果 Customers 資料表中的資料 列已刪除,則會刪除 Orders 資料表中 參考特定資料列的所有資料列

 <Association Name="FK_CustomerOrders">
   <End Role="Customers"
        Type="ExampleModel.Store.Customers" Multiplicity="1">
     <OnDelete Action="Cascade" />
   </End>
   <End Role="Orders"
        Type="ExampleModel.Store.Orders" Multiplicity="*" />
   <ReferentialConstraint>
     <Principal Role="Customers">
       <PropertyRef Name="CustomerId" />
     </Principal>
     <Dependent Role="Orders">
       <PropertyRef Name="CustomerId" />
     </Dependent>
   </ReferentialConstraint>
 </Association>

Parameter 項目 (SSDL)

Store 架構定義語言中的 Parameter 元素 (SSDL) 是 Function 元素的子系,可指定資料庫中預存程式的參數。

Parameter 元素可以有下列子項目(依列出的順序):

  • 檔 (零或一個)
  • 注釋專案 (零個或多個)

適用屬性

下表描述可套用至 Parameter 元素的屬性。

屬性名稱 是必要的
名稱 Yes 參數名稱。
類型 Yes 參數型別。
模式 No 根據參數是輸入、 輸出或輸入/輸出參數而定,InOut 或 InOut
MaxLength No 參數的長度上限。
有效位數 No 參數的精確度。
縮放比例 No 參數的小數位數。
SRID No 空間系統參考識別碼。 僅適用于空間類型的參數。 如需詳細資訊,請參閱 SRID SRID (SQL Server)

注意

任何數目的注釋屬性(自訂 XML 屬性)都可以套用至 Parameter 元素。 不過,自訂屬性不可屬於任何 XML 命名空間,這是保留供 SSDL 使用。 任兩個自訂屬性的完整名稱不能相同。

範例

下列範例顯示具有兩 Parameter 元素的 Function 元素,可指定輸入參數:

 <Function Name="UpdateOrderQuantity"
           Aggregate="false"
           BuiltIn="false"
           NiladicFunction="false"
           IsComposable="false"
           ParameterTypeSemantics="AllowImplicitConversion"
           Schema="dbo">
   <Parameter Name="orderId" Type="int" Mode="In" />
   <Parameter Name="newQuantity" Type="int" Mode="In" />
 </Function>

Principal 項目 (SSDL)

存放區架構定義語言中的 Principal 元素 (SSDL) 是 ReferentialConstraint 元素的子項目,可定義外鍵條件約束的主體結尾(也稱為引用條件約束)。 Principal 元素會指定另一個資料行(或資料行)所參考之資料表中的主鍵資料行(或資料行)。 PropertyRef 元素會指定參考哪些資料行。 Dependent 元素會指定參考 Principal 元素中所 指定之主鍵資料行的資料行。

Principal 元素可以有下列子項目(依所列的順序):

  • PropertyRef (一或多個)
  • 注釋專案 (零個或多個)

適用屬性

下表描述可套用至 Principal 元素的屬性。

屬性名稱 是必要的
角色 Yes 對應 End 元素的 Role 屬性相同值,否則為包含參考資料行的資料表名稱。

注意

任何數目的注釋屬性(自訂 XML 屬性)都可以套用至 Principal 元素。 不過,自訂屬性不可屬於任何 XML 命名空間,這是保留給 CSDL 之用。 任兩個自訂屬性的完整名稱不能相同。

範例

下列範例顯示使用 ReferentialConstraint 元素來指定參與FK_CustomerOrders 外鍵條件約束的資料行的 Association 元素。 Principal 元素會將 Customer 資料表的 CustomerId 資料行指定為條件約束的主體結尾。

 <Association Name="FK_CustomerOrders">
   <End Role="Customers"
        Type="ExampleModel.Store.Customers" Multiplicity="1">
     <OnDelete Action="Cascade" />
   </End>
   <End Role="Orders"
        Type="ExampleModel.Store.Orders" Multiplicity="*" />
   <ReferentialConstraint>
     <Principal Role="Customers">
       <PropertyRef Name="CustomerId" />
     </Principal>
     <Dependent Role="Orders">
       <PropertyRef Name="CustomerId" />
     </Dependent>
   </ReferentialConstraint>
 </Association>

Property 項目 (SSDL)

存放區架構定義語言中的 Property 元素 (SSDL) 代表基礎資料庫中資料表中的資料行。 屬性 元素是 EntityType 元素的子系,代表資料表中的資料列。 EntityType 元素上 定義的每個 Property 元素都代表一個資料行。

Property 元素不能有任何子專案。

適用屬性

下表描述可套用至 Property 元素的屬性。

屬性名稱 是必要的
名稱 Yes 對應資料行的名稱。
類型 Yes 對應資料行的類型。
可為 Null No True (預設值)或 False ,視對應的資料行是否可以有 Null 值而定。
DefaultValue No 對應資料行的預設值。
MaxLength No 對應資料行的長度上限。
FixedLength No True False ,視對應的資料行值是否儲存為固定長度字串而定。
有效位數 No 對應資料行的精確度。
縮放比例 No 對應資料行的小數位數。
Unicode No True False ,視對應的資料行值是否會儲存為 Unicode 字串而定。
定序 No 指定資料來源中使用之定序順序的字串。
SRID No 空間系統參考識別碼。 僅適用于空間類型的屬性。 如需詳細資訊,請參閱 SRID SRID (SQL Server)
StoreGeneratedPattern No None Identity (如果對應的資料行值是資料庫中產生的識別),或 Computed (如果資料庫中計算對應的資料行值)。 對 RowType 屬性無效。

注意

任何數目的注釋屬性(自訂 XML 屬性)都可以套用至 Property 元素。 不過,自訂屬性不可屬於任何 XML 命名空間,這是保留供 SSDL 使用。 任兩個自訂屬性的完整名稱不能相同。

範例

下列範例顯示具有兩個 Property 元素的 EntityType 元素:

 <EntityType Name="Customers">
   <Documentation>
     <Summary>Summary here.</Summary>
     <LongDescription>Long description here.</LongDescription>
   </Documentation>
   <Key>
     <PropertyRef Name="CustomerId" />
   </Key>
   <Property Name="CustomerId" Type="int" Nullable="false" />
   <Property Name="Name" Type="nvarchar(max)" Nullable="false" />
 </EntityType>

PropertyRef 項目 (SSDL)

Store 架構定義語言中的 PropertyRef 元素會參考 EntityType 元素上定義的屬性,指出屬性會執行下列其中一個角色:

  • 成為 EntityType 所代表資料表 主鍵的一部分。 一或多個 PropertyRef 元素可用來定義主鍵。 如需詳細資訊,請參閱 Key 元素。
  • 做為參考條件約束的相依端點和主要端點。 如需詳細資訊,請參閱 ReferentialConstraint 元素。

PropertyRef 元素只能有下列子項目:

  • 檔 (零或一個)
  • Annotation 項目

適用屬性

下表描述可套用至 PropertyRef 元素的屬性。

屬性名稱 是必要的
名稱 Yes 參考屬性的名稱。

注意

任何數目的注釋屬性(自訂 XML 屬性)都可以套用至 PropertyRef 元素。 不過,自訂屬性不可屬於任何 XML 命名空間,這是保留給 CSDL 之用。 任兩個自訂屬性的完整名稱不能相同。

範例

下列範例顯示 PropertyRef 元素,用來藉由參考 EntityType 元素上 定義的屬性來定義主鍵。

 <EntityType Name="Customers">
   <Documentation>
     <Summary>Summary here.</Summary>
     <LongDescription>Long description here.</LongDescription>
   </Documentation>
   <Key>
     <PropertyRef Name="CustomerId" />
   </Key>
   <Property Name="CustomerId" Type="int" Nullable="false" />
   <Property Name="Name" Type="nvarchar(max)" Nullable="false" />
 </EntityType>

ReferentialConstraint 項目 (SSDL)

存放區架構定義語言中的 ReferentialConstraint 元素 (SSDL) 代表基礎資料庫中的外鍵條件約束(也稱為參考完整性條件約束)。 條件約束的主體和相依端分別由 Principal 和 Dependent 子專案指定。 參與主體和相依端的資料行會參考 PropertyRef 元素。

ReferentialConstraint 元素是 Association 元素的選擇性子項目。 如果未使用 ReferentialConstraint 元素來對應 Association 元素中指定的 外鍵條件約束,則必須使用 AssociationSetMapping 元素來執行此動作。

ReferentialConstraint 元素可以有下列子項目:

  • 檔 (零或一個)
  • 主體(正好一個)
  • 相依 (正好一個)
  • 注釋專案 (零個或多個)

適用屬性

任何數目的注釋屬性(自訂 XML 屬性)都可以套用至 ReferentialConstraint 元素。 不過,自訂屬性不可屬於任何 XML 命名空間,這是保留供 SSDL 使用。 任兩個自訂屬性的完整名稱不能相同。

範例

下列範例顯示 使用 ReferentialConstraint 元素來指定參與FK_CustomerOrders 外鍵條件約束的資料行的 Association 元素:

 <Association Name="FK_CustomerOrders">
   <End Role="Customers"
        Type="ExampleModel.Store.Customers" Multiplicity="1">
     <OnDelete Action="Cascade" />
   </End>
   <End Role="Orders"
        Type="ExampleModel.Store.Orders" Multiplicity="*" />
   <ReferentialConstraint>
     <Principal Role="Customers">
       <PropertyRef Name="CustomerId" />
     </Principal>
     <Dependent Role="Orders">
       <PropertyRef Name="CustomerId" />
     </Dependent>
   </ReferentialConstraint>
 </Association>

ReturnType 元素 (SSDL)

存放區架構定義語言 (SSDL) 中的 ReturnType 元素會指定 Function 元素中定義之函式的 傳回型別。 您也可以使用 ReturnType 屬性來指定函式傳回型別。

函式的傳回型別是以 Type 屬性或 ReturnType 元素指定

ReturnType 元素可以有下列子項目:

  • CollectionType (一)

注意

任何數目的注釋屬性(自訂 XML 屬性)都可以套用至 ReturnType 元素。 不過,自訂屬性不可屬於任何 XML 命名空間,這是保留供 SSDL 使用。 任兩個自訂屬性的完整名稱不能相同。

範例

下列範例使用 會傳回資料列集合的 Function

   <Function Name="GetProducts" IsComposable="true" Schema="dbo">
     <ReturnType>
       <CollectionType>
         <RowType>
           <Property Name="ProductID" Type="int" Nullable="false" />
           <Property Name="CategoryID" Type="bigint" Nullable="false" />
           <Property Name="ProductName" Type="nvarchar" MaxLength="40" Nullable="false" />
           <Property Name="UnitPrice" Type="money" />
           <Property Name="Discontinued" Type="bit" />
         </RowType>
       </CollectionType>
     </ReturnType>
   </Function>

RowType 元素 (SSDL)

存放區架構定義語言 (SSDL) 中的 RowType 元素會將未命名的結構定義為存放區中定義之函式的傳回型別。

RowType 元素是 CollectionType 元素的 子項目:

RowType 元素可以有下列子項目:

  • 屬性 (一或多個)

範例

下列範例顯示使用 CollectionType 元素指定函數傳回資料列集合的 store 函式(如 RowType 元素中所 指定)。

   <Function Name="GetProducts" IsComposable="true" Schema="dbo">
     <ReturnType>
       <CollectionType>
         <RowType>
           <Property Name="ProductID" Type="int" Nullable="false" />
           <Property Name="CategoryID" Type="bigint" Nullable="false" />
           <Property Name="ProductName" Type="nvarchar" MaxLength="40" Nullable="false" />
           <Property Name="UnitPrice" Type="money" />
           <Property Name="Discontinued" Type="bit" />
         </RowType>
       </CollectionType>
     </ReturnType>
   </Function>

Schema 項目 (SSDL)

存放區架構定義語言中的 Schema 元素 (SSDL) 是儲存體模型定義的根項目。 其中包含組成儲存模型的物件、函式和容器等定義。

Schema 元素可能包含下列子項目的零或多個:

  • 關聯
  • EntityType
  • EntityContainer
  • 函式

Schema 元素會使用 Namespace 屬性來定義儲存體模型中實體類型和關聯物件的命名空間。 在命名空間中,兩個物件不能有相同的名稱。

儲存體模型命名空間與 Schema 元素的 XML 命名空間不同。 儲存體模型命名空間(如 Namespace 屬性所定義 )是實體類型和關聯類型的邏輯容器。 Schema 元素的 XML 命名空間(以 xmlns 屬性工作表示)是 Schema 元素之子專案和屬性 的預設命名空間。 格式 https://schemas.microsoft.com/ado/YYYY/MM/edm/ssdl 的 XML 命名空間(其中 YYYY 和 MM 分別代表年和月)保留給 SSDL。 自訂項目和屬性不能出現在擁有此格式的命名空間中。

適用屬性

下表描述屬性可以套用至 Schema 元素。

屬性名稱 是必要的
Namespace Yes 儲存模型的命名空間。 Namespace 屬性的值 是用來形成型別的完整名稱。 例如,如果 名為 Customer 的 EntityType 位於 ExampleModel.Store 命名空間中,則 EntityType 的完整名稱 為 ExampleModel.Store.Customer。
下列字串無法做為 Namespace 屬性的值 系統 暫時性 Edm 。 Namespace 屬性的值 不能與 CSDL Schema 元素中 Namespace 屬性的值 相同。
別名 No 用來取代命名空間名稱的識別項。 例如,如果名為 Customer 的 EntityType 位於 ExampleModel.Store 命名空間中,且 Alias 屬性 的值 是 儲存體Model,則您可以使用 儲存體Model.Customer 作為 EntityType 的完整名稱
提供者 Yes 資料提供者。
ProviderManifestToken Yes 向提供者表示要傳回哪個提供者資訊清單的語彙基元。 未定義此語彙基元的格式。 語彙基元的值是由提供者定義。 如需 SQL Server 提供者資訊清單權杖的相關資訊,請參閱 Entity Framework 的 SqlClient。

範例

下列範例顯示包含 EntityContainer 元素、兩 EntityType 元素和一個 Association 元素的 Schema 元素。

 <Schema Namespace="ExampleModel.Store"
       Alias="Self" Provider="System.Data.SqlClient"
       ProviderManifestToken="2008"
       xmlns="https://schemas.microsoft.com/ado/2009/11/edm/ssdl">
   <EntityContainer Name="ExampleModelStoreContainer">
     <EntitySet Name="Customers"
                EntityType="ExampleModel.Store.Customers"
                Schema="dbo" />
     <EntitySet Name="Orders"
                EntityType="ExampleModel.Store.Orders"
                Schema="dbo" />
     <AssociationSet Name="FK_CustomerOrders"
                     Association="ExampleModel.Store.FK_CustomerOrders">
       <End Role="Customers" EntitySet="Customers" />
       <End Role="Orders" EntitySet="Orders" />
     </AssociationSet>
   </EntityContainer>
   <EntityType Name="Customers">
     <Documentation>
       <Summary>Summary here.</Summary>
       <LongDescription>Long description here.</LongDescription>
     </Documentation>
     <Key>
       <PropertyRef Name="CustomerId" />
     </Key>
     <Property Name="CustomerId" Type="int" Nullable="false" />
     <Property Name="Name" Type="nvarchar(max)" Nullable="false" />
   </EntityType>
   <EntityType Name="Orders" xmlns:c="http://CustomNamespace">
     <Key>
       <PropertyRef Name="OrderId" />
     </Key>
     <Property Name="OrderId" Type="int" Nullable="false"
               c:CustomAttribute="someValue"/>
     <Property Name="ProductId" Type="int" Nullable="false" />
     <Property Name="Quantity" Type="int" Nullable="false" />
     <Property Name="CustomerId" Type="int" Nullable="false" />
     <c:CustomElement>
       Custom data here.
     </c:CustomElement>
   </EntityType>
   <Association Name="FK_CustomerOrders">
     <End Role="Customers"
          Type="ExampleModel.Store.Customers" Multiplicity="1">
       <OnDelete Action="Cascade" />
     </End>
     <End Role="Orders"
          Type="ExampleModel.Store.Orders" Multiplicity="*" />
     <ReferentialConstraint>
       <Principal Role="Customers">
         <PropertyRef Name="CustomerId" />
       </Principal>
       <Dependent Role="Orders">
         <PropertyRef Name="CustomerId" />
       </Dependent>
     </ReferentialConstraint>
   </Association>
   <Function Name="UpdateOrderQuantity"
             Aggregate="false"
             BuiltIn="false"
             NiladicFunction="false"
             IsComposable="false"
             ParameterTypeSemantics="AllowImplicitConversion"
             Schema="dbo">
     <Parameter Name="orderId" Type="int" Mode="In" />
     <Parameter Name="newQuantity" Type="int" Mode="In" />
   </Function>
   <Function Name="UpdateProductInOrder" IsComposable="false">
     <CommandText>
       UPDATE Orders
       SET ProductId = @productId
       WHERE OrderId = @orderId;
     </CommandText>
     <Parameter Name="productId"
                Mode="In"
                Type="int"/>
     <Parameter Name="orderId"
                Mode="In"
                Type="int"/>
   </Function>
 </Schema>

註釋屬性

存放結構定義語言 (SSDL) 中的 Annotation 屬性是儲存體模型中的自訂 XML 屬性,它們可提供與儲存體模型中之項目相關的額外中繼資料。 除了擁有有效的 XML 結構外,下列條件約束適用於 Annotation 屬性:

  • Annotation 屬性不能在任何 XML 命名空間之中,這是保留供 SSDL 之用。
  • 任兩個 Annotation 屬性的完整名稱不能相同。

可以將一個以上的 Annotation 屬性套用至指定的 SSDL 項目。 您可以使用 System.Data.Metadata.Edm 命名空間中的類別,在執行時間存取注釋元素中包含的中繼資料。

範例

下列範例顯示 EntityType 元素,該元素已套用至 OrderId 屬性的 注釋屬性。 此範例也會顯示新增至 EntityType 元素的 注釋專案。

 <EntityType Name="Orders" xmlns:c="http://CustomNamespace">
   <Key>
     <PropertyRef Name="OrderId" />
   </Key>
   <Property Name="OrderId" Type="int" Nullable="false"
             c:CustomAttribute="someValue"/>
   <Property Name="ProductId" Type="int" Nullable="false" />
   <Property Name="Quantity" Type="int" Nullable="false" />
   <Property Name="CustomerId" Type="int" Nullable="false" />
   <c:CustomElement>
     Custom data here.
   </c:CustomElement>
 </EntityType>

Annotation 項目 (SSDL)

存放結構定義語言 (SSDL) 中的 Annotation 項目是儲存體模型中的自訂 XML 項目,它們可提供與儲存體模型相關的額外中繼資料。 除了擁有有效的 XML 結構外,下列條件約束適用於 Annotation 項目:

  • Annotation 項目不能存在於保留供 SSDL 使用的任何 XML 命名空間中。
  • 任兩個 Annotation 項目的完整名稱不能相同。
  • Annotation 項目必須出現在所指定 SSDL 項目的所有其他子項目之後。

多個 Annotation 項目可以同時做為所指定 SSDL 項目的子系。 從 .NET Framework 第 4 版開始,您可以使用 System.Data.Metadata.Edm 命名空間中的類別,在執行時間存取注釋元素中包含的中繼資料。

範例

下列範例顯示具有注釋專案 ( CustomElement ) 的 EntityType 元素。 此範例也會顯示套用至 OrderId 屬性的 注釋屬性。

 <EntityType Name="Orders" xmlns:c="http://CustomNamespace">
   <Key>
     <PropertyRef Name="OrderId" />
   </Key>
   <Property Name="OrderId" Type="int" Nullable="false"
             c:CustomAttribute="someValue"/>
   <Property Name="ProductId" Type="int" Nullable="false" />
   <Property Name="Quantity" Type="int" Nullable="false" />
   <Property Name="CustomerId" Type="int" Nullable="false" />
   <c:CustomElement>
     Custom data here.
   </c:CustomElement>
 </EntityType>

Facet (SSDL)

存放區架構定義語言 (SSDL) 中的 Facet 代表 Property 元素中所指定資料行類型的條件約束。 Facet 會在 Property 元素上 實作為 XML 屬性。

下表說明 SSDL 中支援的 Facet:

Facet 描述
定序 在執行比較和排序屬性值的作業時,指定要使用的定序順序 (或排序順序)。
FixedLength 指定資料行值的長度是否可以變更。
MaxLength 指定資料行值的最大長度。
有效位數 針對 Decimal 類型的 屬性,指定屬性值可以擁有的位數。 針對 Time 、DateTime 和 DateTimeOffset 類型的 屬性,指定資料行值小數秒數的數位 數。
縮放比例 指定資料行值小數點右邊的位數。
Unicode 指定資料行值是否儲存為 Unicode。