SSDL 规范SSDL Specification

存储架构定义语言 (SSDL) 是一种基于 XML 的语言,用于描述实体框架应用程序的存储模型。Store schema definition language (SSDL) is an XML-based language that describes the storage model of an Entity Framework application.

在实体框架应用程序中,将使用在 StoreItemCollection 中) 编写的 ssdl 文件 (将存储模型元数据加载到,并可通过使用 system.exception 类中的方法来访问该实例。In an Entity Framework application, storage model metadata is loaded from a .ssdl file (written in SSDL) into an instance of the System.Data.Metadata.Edm.StoreItemCollection and is accessible by using methods in the System.Data.Metadata.Edm.MetadataWorkspace class. 实体框架使用存储模型元数据将针对概念模型的查询转换为特定于存储的命令。Entity Framework uses storage model metadata to translate queries against the conceptual model to store-specific commands.

Entity Framework Designer (EF 设计器) 在设计时将存储模型信息存储在 .edmx 文件中。The Entity Framework Designer (EF Designer) stores storage model information in an .edmx file at design time. 在生成时,Entity Designer 使用 .edmx 文件中的信息来创建运行时实体框架所需的 ssdl 文件。At build time the Entity Designer uses information in an .edmx file to create the .ssdl file that is needed by Entity Framework at runtime.

SSDL 的版本按 XML 命名空间进行区分。Versions of SSDL are differentiated by XML namespaces.

SSDL 版本SSDL Version XML 命名空间XML Namespace
SSDL v1SSDL v1 https://schemas.microsoft.com/ado/2006/04/edm/ssdl
SSDL v2SSDL v2 https://schemas.microsoft.com/ado/2009/02/edm/ssdl
SSDL v3SSDL v3 https://schemas.microsoft.com/ado/2009/11/edm/ssdl

Association 元素 (SSDL)Association Element (SSDL)

存储架构定义语言 (SSDL) 中的 Association 元素指定参与基础数据库中外键约束的表列。An Association element in store schema definition language (SSDL) specifies table columns that participate in a foreign key constraint in the underlying database. 两个必需的子 End 元素指定位于关联两端的表和各端的重数。Two required child End elements specify tables at the ends of the association and the multiplicity at each end. 一个可选的 ReferentialConstraint 元素指定关联的主体端和依赖端以及参与列。An optional ReferentialConstraint element specifies the principal and dependent ends of the association as well as the participating columns. 如果不存在 ReferentialConstraint 元素,则必须使用 AssociationSetMapping 元素来指定关联的列映射。If no ReferentialConstraint element is present, an AssociationSetMapping element must be used to specify the column mappings for the association.

Association元素可以具有以下子元素 (按列出的顺序) :The Association element can have the following child elements (in the order listed):

  • Documentation(零个或一个)Documentation (zero or one)
  • End(正好两个)End (exactly two)
  • ReferentialConstraint(零个或一个)ReferentialConstraint (zero or one)
  • 批注元素(零个或多个)Annotation elements (zero or more)

适用的属性Applicable Attributes

下表介绍可应用于 Association 元素的特性。The following table describes the attributes that can be applied to the Association element.

属性名称Attribute Name 是否必需Is Required Value
名称Name Yes 基础数据库中相应外键约束的名称。The name of the corresponding foreign key constraint in the underlying database.

备注

可以将任意数量的批注属性 (自定义 XML 特性) 应用于 Association 元素。Any number of annotation attributes (custom XML attributes) may be applied to the Association element. 然而,自定义特性可能不属于为 SSDL 保留的任何 XML 命名空间。However, custom attributes may not belong to any XML namespace that is reserved for SSDL. 任何两个自定义特性的完全限定名称都不能相同。The fully-qualified names for any two custom attributes cannot be the same.

示例Example

下面的示例演示一个 Association 元素,该元素使用 ReferentialConstraint 元素指定参与 FK _ CustomerOrders 外键约束的列:The following example shows an Association element that uses a ReferentialConstraint element to specify the columns that participate in the FK_CustomerOrders foreign key constraint:

 <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)AssociationSet Element (SSDL)

存储架构定义语言 (SSDL) 中的 AssociationSet 元素表示基础数据库中两个表之间的外键约束。The AssociationSet element in store schema definition language (SSDL) represents a foreign key constraint between two tables in the underlying database. 参与外键约束的表列在 Association 元素中指定。The table columns that participate in the foreign key constraint are specified in an Association element. 与给定associationset元素对应的association元素是在AssociationSet元素的association特性中指定的。The Association element that corresponds to a given AssociationSet element is specified in the Association attribute of the AssociationSet element.

SSDL 关联集通过 AssociationSetMapping 元素映射到 CSDL 关联集。SSDL association sets are mapped to CSDL association sets by an AssociationSetMapping element. 但是,如果使用 ReferentialConstraint 元素定义给定 CSDL 关联集的 CSDL 关联,则不需要相应的 AssociationSetMapping 元素。However, if the CSDL association for a given CSDL association set is defined by using a ReferentialConstraint element , no corresponding AssociationSetMapping element is necessary. 在这种情况下,如果存在 AssociationSetMapping 元素,该元素定义的映射将被 ReferentialConstraint 元素重写。In this case, if an AssociationSetMapping element is present, the mappings it defines will be overridden by the ReferentialConstraint element.

AssociationSet元素可以具有以下子元素 (按列出的顺序) :The AssociationSet element can have the following child elements (in the order listed):

  • Documentation(零个或一个)Documentation (zero or one)
  • End(零个或两个)End (zero or two)
  • 批注元素(零个或多个)Annotation elements (zero or more)

适用的属性Applicable Attributes

下表介绍可应用于 AssociationSet 元素的特性。The following table describes the attributes that can be applied to the AssociationSet element.

属性名称Attribute Name 是否必需Is Required Value
名称Name Yes 关联集表示的外键约束的名称。The name of the foreign key constraint that the association set represents.
关联Association Yes 定义参与外键约束的列的关联的名称。The name of the association that defines the columns that participate in the foreign key constraint.

备注

任意数量的批注属性 (自定义 XML 特性) 可以应用于 AssociationSet 元素。Any number of annotation attributes (custom XML attributes) may be applied to the AssociationSet element. 然而,自定义特性可能不属于为 SSDL 保留的任何 XML 命名空间。However, custom attributes may not belong to any XML namespace that is reserved for SSDL. 任何两个自定义特性的完全限定名称都不能相同。The fully-qualified names for any two custom attributes cannot be the same.

示例Example

下面的示例演示了一个 AssociationSet 元素,该元素表示 FK_CustomerOrders 基础数据库中的外键约束:The following example shows an AssociationSet element that represents the FK_CustomerOrders foreign key constraint in the underlying database:

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

CollectionType 元素 (SSDL) CollectionType Element (SSDL)

存储架构定义语言 (SSDL) 中的 CollectionType 元素指定函数的返回类型是集合。The CollectionType element in store schema definition language (SSDL) specifies that a function’s return type is a collection. CollectionType元素是 ReturnType 元素的子元素。The CollectionType element is a child of the ReturnType element. 集合的类型是通过使用 RowType 子元素指定的:The type of collection is specified by using the RowType child element:

备注

可以将任意数量的批注属性 (自定义 XML 特性) 应用于 CollectionType 元素。Any number of annotation attributes (custom XML attributes) may be applied to the CollectionType element. 然而,自定义特性可能不属于为 SSDL 保留的任何 XML 命名空间。However, custom attributes may not belong to any XML namespace that is reserved for SSDL. 任何两个自定义特性的完全限定名称都不能相同。The fully-qualified names for any two custom attributes cannot be the same.

示例Example

下面的示例演示一个函数,该函数使用 CollectionType 元素来指定该函数返回行的集合。The following example shows a function that uses a CollectionType element to specify that the function returns a collection of rows.

   <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)CommandText Element (SSDL)

存储架构定义语言 (SSDL) 中的 CommandText 元素是 Function 元素的子元素,它允许您定义在数据库中执行的 SQL 语句。The CommandText element in store schema definition language (SSDL) is a child of the Function element that allows you to define a SQL statement that is executed at the database. CommandText元素允许添加类似于数据库中的存储过程的功能,但在存储模型中定义CommandText元素。The CommandText element allows you to add functionality that is similar to a stored procedure in the database, but you define the CommandText element in the storage model.

CommandText元素不能有子元素。The CommandText element cannot have child elements. CommandText元素的正文必须是基础数据库的有效 SQL 语句。The body of the CommandText element must be a valid SQL statement for the underlying database.

无特性适用于 CommandText 元素。No attributes are applicable to the CommandText element.

示例Example

下面的示例演示具有子CommandText元素的函数元素。The following example shows a Function element with a child CommandText element. 通过将 UpdateProductInOrder 函数导入到概念模型中,将其作为 ObjectContext 的方法公开。Expose the UpdateProductInOrder function as a method on the ObjectContext by importing it into the conceptual model.  

 <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)DefiningQuery Element (SSDL)

使用存储架构定义语言 (SSDL) 中的 DefiningQuery 元素,你可以直接在基础数据库中执行 SQL 语句。The DefiningQuery element in store schema definition language (SSDL) allows you to execute a SQL statement directly in the underlying database. DefiningQuery元素通常用于数据库视图,但该视图是在存储模型中定义的,而不是在数据库中定义的。The DefiningQuery element is commonly used like a database view, but the view is defined in the storage model instead of the database. DefiningQuery 元素中定义的视图可以通过 EntitySetMapping 元素映射到概念模型中的实体类型。The view defined in a DefiningQuery element can be mapped to an entity type in the conceptual model through an EntitySetMapping element. 这些映射是只读的。These mappings are read-only.  

下面的 SSDL 语法显示了 EntitySet 的声明,后跟包含用于检索视图的查询的 DefiningQuery 元素。The following SSDL syntax shows the declaration of an EntitySet followed by the DefiningQuery element that contains a query used to retrieve the view.

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

您可以使用实体框架中的存储过程来对视图启用读写方案。You can use stored procedures in the Entity Framework to enable read-write scenarios over views.您可以使用数据源视图或实体 SQL 视图作为基表来检索数据和存储过程的更改处理。 You can use either a data source view or an Entity SQL view as the base table for retrieving data and for change processing by stored procedures.

你可以使用 DefiningQuery 元素将 Microsoft SQL Server Compact 为3.5。You can use the DefiningQuery element to target Microsoft SQL Server Compact 3.5. 尽管 SQL Server Compact 3.5 不支持存储过程,但你可以通过 DefiningQuery 元素实现类似功能。Though SQL Server Compact 3.5 does not support stored procedures, you can implement similar functionality with the DefiningQuery element. 另一个有用之处在于,可以创建存储过程以克服编程语言中使用的数据类型与数据源的数据类型的不匹配。Another place where it can be useful is in creating stored procedures to overcome a mismatch between the data types used in the programming language and those of the data source. 您可以编写一个使用一组特定参数的 DefiningQuery ,然后使用一组不同的参数调用存储过程,例如,删除数据的存储过程。You could write a DefiningQuery that takes a certain set of parameters and then calls a stored procedure with a different set of parameters, for example, a stored procedure that deletes data.

Dependent 元素 (SSDL)Dependent Element (SSDL)

存储架构定义语言 (SSDL) 中的 依赖 元素是 ReferentialConstraint 元素的子元素,用于定义外键约束 (也称为引用约束) 的依赖端。The Dependent element in store schema definition language (SSDL) is a child element to the ReferentialConstraint element that defines the dependent end of a foreign key constraint (also called a referential constraint). Dependent元素指定表中) (或列的列,这些列引用主键列 (或列) 。The Dependent element specifies the column (or columns) in a table that reference a primary key column (or columns). PropertyRef 元素指定要引用的列。PropertyRef elements specify which columns are referenced. 主体元素指定 依赖 元素中指定的列所引用的主键列。The Principal element specifies the primary key columns that are referenced by columns that are specified in the Dependent element.

依赖元素可以具有以下子元素 (按列出的顺序) :The Dependent element can have the following child elements (in the order listed):

  • PropertyRef(一个或多个)PropertyRef (one or more)
  • 批注元素(零个或多个)Annotation elements (zero or more)

适用的属性Applicable Attributes

下表介绍可应用于 依赖 元素的特性。The following table describes the attributes that can be applied to the Dependent element.

属性名称Attribute Name 是否必需Is Required “值”Value
角色Role Yes Role 特性相同的值 (如果使用) 对应的结束元素,则为; 否则为。否则为包含引用列的表的名称。The same value as the Role attribute (if used) of the corresponding End element; otherwise, the name of the table that contains the referencing column.

备注

可以将任意数量的批注属性 (自定义 XML 特性) 应用于 依赖 元素。Any number of annotation attributes (custom XML attributes) may be applied to the Dependent element. 然而,自定义特性可能不属于为 CSDL 保留的任何 XML 命名空间。However, custom attributes may not belong to any XML namespace that is reserved for CSDL. 任何两个自定义特性的完全限定名称都不能相同。The fully-qualified names for any two custom attributes cannot be the same.

示例Example

下面的示例演示一个 Association 元素,该元素使用 ReferentialConstraint 元素指定参与 FK _ CustomerOrders 外键约束的列。The following example shows an Association element that uses a ReferentialConstraint element to specify the columns that participate in the FK_CustomerOrders foreign key constraint. Dependent元素将Order表的CustomerId列指定为约束的依赖端。The Dependent element specifies the CustomerId column of the Order table as the dependent end of the constraint.

 <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)Documentation Element (SSDL)

存储架构定义语言 (SSDL) 中的 文档 元素可用于提供有关在父元素中定义的对象的信息。The Documentation element in store schema definition language (SSDL) can be used to provide information about an object that is defined in a parent element.

文档元素可以具有以下子元素 (按列出的顺序) :The Documentation element can have the following child elements (in the order listed):

  • 摘要:父元素的简要说明。Summary: A brief description of the parent element. (零个或一个元素)(zero or one element)
  • LongDescription:父元素的详细说明。LongDescription: An extensive description of the parent element. (零个或一个元素)(zero or one element)

适用的属性Applicable Attributes

可以将任意数量的批注属性 (自定义 XML 特性) 可以应用于 文档 元素。Any number of annotation attributes (custom XML attributes) may be applied to the Documentation element. 然而,自定义特性可能不属于为 CSDL 保留的任何 XML 命名空间。However, custom attributes may not belong to any XML namespace that is reserved for CSDL. 任何两个自定义特性的完全限定名称都不能相同。The fully-qualified names for any two custom attributes cannot be the same.

示例Example

下面的示例将 文档 元素显示为 EntityType 元素的子元素。The following example shows the Documentation element as a child element of an EntityType element.

 <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)End Element (SSDL)

存储架构定义语言中的 结束 元素 (SSDL) 指定基础数据库中外键约束一端的表和行数。The End element in store schema definition language (SSDL) specifies the table and number of rows at one end of a foreign key constraint in the underlying database. End元素可以是 Association 元素或 AssociationSet 元素的子元素。The End element can be a child of the Association element or the AssociationSet element. 在每种情况下,可能的子元素以及适用的特性都不同。In each case, the possible child elements and applicable attributes are different.

End 元素作为 Association 元素的子元素End Element as a Child of the Association Element

End元素 (作为Association元素的子元素) 使用Type多重性特性指定 foreign key 约束末尾的表和行数。An End element (as a child of the Association element) specifies the table and number of rows at the end of a foreign key constraint with the Type and Multiplicity attributes respectively. 外键约束的两端定义为 SSDL 关联的一部分;SSDL 关联必须仅具有两端。Ends of a foreign key constraint are defined as part of an SSDL association; an SSDL association must have exactly two ends.

End元素可以具有以下子元素 (按列出的顺序) :An End element can have the following child elements (in the order listed):

  • Documentation(零个或一个元素)Documentation (zero or one element)
  • OnDelete(零个或一个元素)OnDelete (zero or one element)
  • 批注元素(零个或多个元素)Annotation elements (zero or more elements)

适用的属性Applicable Attributes

下表描述了当 结束 元素是 Association 元素的子元素时,可应用于该元素的特性。The following table describes the attributes that can be applied to the End element when it is the child of an Association element.

属性名称Attribute Name 是否必需Is Required “值”Value
类型Type Yes 处于外键约束端的 SSDL 实体集的完全限定名。The fully qualified name of the SSDL entity set that is at the end of the foreign key constraint.
角色Role No 如果) 使用,则为相应 ReferentialConstraint (元素的主体或从属元素中的 Role 特性的值。The value of the Role attribute in either the Principal or Dependent element of the corresponding ReferentialConstraint element (if used).
多重性Multiplicity Yes 10 或 1或, * 具体取决于外键约束末尾的行数。1, 0..1, or * depending on the number of rows that can be at the end of the foreign key constraint.
1 指示在外键约束端刚好存在一行。1 indicates that exactly one row exists at the foreign key constraint end.
0 .0 表示外键约束端处存在零个或一行。0..1 indicates that zero or one row exists at the foreign key constraint end.
* 指示外键约束端处存在零行、一行或多行。* indicates that zero, one, or more rows exist at the foreign key constraint end.

备注

可以将任意数量的批注属性 (自定义 XML 特性) 应用于 结束 元素。Any number of annotation attributes (custom XML attributes) may be applied to the End element. 然而,自定义特性可能不属于为 CSDL 保留的任何 XML 命名空间。However, custom attributes may not belong to any XML namespace that is reserved for CSDL. 任何两个自定义特性的完全限定名称都不能相同。The fully-qualified names for any two custom attributes cannot be the same.

示例Example

下面的示例演示了一个定义FK _ CustomerOrders外键约束的Association元素。The following example shows an Association element that defines the FK_CustomerOrders foreign key constraint. 在每个End元素上指定的多重性值指示Orders表中的多个行可以与Customers表中的某一行相关联,但customers表中只有一个行可以与Orders表中的一行相关联。The Multiplicity values specified on each End element indicate that many rows in the Orders table can be associated with a row in the Customers table, but only one row in the Customers table can be associated with a row in the Orders table. 此外, OnDelete元素指示如果删除customers表中的行,则将删除Orders表中引用customers表中的特定行的所有行。Additionally, the OnDelete element indicates that all rows in the Orders table that reference a particular row in the Customers table will be deleted if the row in the Customers table is deleted.

 <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 Element as a Child of the AssociationSet Element

End元素 (为AssociationSet元素的子元素) 在基础数据库中的外键约束的一端指定一个表。The End element (as a child of the AssociationSet element) specifies a table at one end of a foreign key constraint in the underlying database.

End元素可以具有以下子元素 (按列出的顺序) :An End element can have the following child elements (in the order listed):

  • Documentation(零个或一个)Documentation (zero or one)
  • 批注元素(零个或多个)Annotation elements (zero or more)

适用的属性Applicable Attributes

下表描述了当 结束 元素为 AssociationSet 元素的子元素时,可应用于该元素的特性。The following table describes the attributes that can be applied to the End element when it is the child of an AssociationSet element.

属性名称Attribute Name 是否必需Is Required “值”Value
EntitySetEntitySet Yes 处于外键约束一端的 SSDL 实体集的名称。The name of the SSDL entity set that is at the end of the foreign key constraint.
角色Role No 在对应的 Association 元素的一个End元素上指定的一个角色特性的值。The value of one of the Role attributes specified on one End element of the corresponding Association element.

备注

可以将任意数量的批注属性 (自定义 XML 特性) 应用于 结束 元素。Any number of annotation attributes (custom XML attributes) may be applied to the End element. 然而,自定义特性可能不属于为 CSDL 保留的任何 XML 命名空间。However, custom attributes may not belong to any XML namespace that is reserved for CSDL. 任何两个自定义特性的完全限定名称都不能相同。The fully-qualified names for any two custom attributes cannot be the same.

示例Example

下面的示例演示一个具有AssociationSet元素的EntityContainer元素,其中包含两个End元素:The following example shows an EntityContainer element with an AssociationSet element with two End elements:

 <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 Element (SSDL)

存储架构定义语言中的 EntityContainer 元素 (SSDL) 描述实体框架应用程序中的基础数据源的结构:在 EntitySet 元素中定义的 ssdl 实体 (集) 表示数据库中的表、在 EntityType 元素中定义的 ssdl 实体类型 (表示表中的行,以及在 AssociationSet 元素中定义的关联集) 表示数据库中的外键约束。An EntityContainer element in store schema definition language (SSDL) describes the structure of the underlying data source in an Entity Framework application: SSDL entity sets (defined in EntitySet elements) represent tables in a database, SSDL entity types (defined in EntityType elements) represent rows in a table, and association sets (defined in AssociationSet elements) represent foreign key constraints in a database. 存储模型实体容器通过 EntityContainerMapping 元素映射到概念模型实体容器。A storage model entity container maps to a conceptual model entity container through the EntityContainerMapping element.

EntityContainer元素可以有零个或一个文档元素。An EntityContainer element can have zero or one Documentation elements. 如果存在 文档 元素,则它必须位于所有其他子元素之前。If a Documentation element is present, it must precede all other child elements.

EntityContainer元素可以具有零个或多个下列子元素 (按列出的顺序) :An EntityContainer element can have zero or more of the following child elements (in the order listed):

  • EntitySetEntitySet
  • AssociationSetAssociationSet
  • 批注元素Annotation elements

适用的属性Applicable Attributes

下表介绍可应用于 EntityContainer 元素的特性。The table below describes the attributes that can be applied to the EntityContainer element.

属性名称Attribute Name 是否必需Is Required Value
名称Name Yes 实体容器的名称。The name of the entity container. 此名称不能包含句点 (.)。This name cannot contain periods (.).

备注

可以将任意数量的批注属性 (自定义 XML 特性) 可以应用于 EntityContainer 元素。Any number of annotation attributes (custom XML attributes) may be applied to the EntityContainer element. 然而,自定义特性可能不属于为 SSDL 保留的任何 XML 命名空间。However, custom attributes may not belong to any XML namespace that is reserved for SSDL. 任何两个自定义特性的完全限定名称都不能相同。The fully-qualified names for any two custom attributes cannot be the same.

示例Example

下面的示例演示一个 EntityContainer 元素,该元素定义了两个实体集和一个关联集。The following example shows an EntityContainer element that defines two entity sets and one association set. 注意,实体类型和关联类型名称由概念模型命名空间名称限定。Note that entity type and association type names are qualified by the conceptual model namespace name.

 <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)EntitySet Element (SSDL)

存储架构定义语言 (SSDL) 中的 EntitySet 元素表示基础数据库中的表或视图。An EntitySet element in store schema definition language (SSDL) represents a table or view in the underlying database. 以 SSDL 表示的 EntityType 元素表示表或视图中的行。An EntityType element in SSDL represents a row in the table or view. EntitySet元素的EntityType特性指定了表示 ssdl 实体集中的行的特定 SSDL 实体类型。The EntityType attribute of an EntitySet element specifies the particular SSDL entity type that represents rows in an SSDL entity set. CSDL 实体集和 SSDL 实体集之间的映射在 EntitySetMapping 元素中指定。The mapping between a CSDL entity set and an SSDL entity set is specified in an EntitySetMapping element.

EntitySet元素可以具有以下子元素 (按列出的顺序) :The EntitySet element can have the following child elements (in the order listed):

  • Documentation(零个或一个元素)Documentation (zero or one element)
  • DefiningQuery(零个或一个元素)DefiningQuery (zero or one element)
  • 批注元素Annotation elements

适用的属性Applicable Attributes

下表介绍可应用于 EntitySet 元素的特性。The following table describes the attributes that can be applied to the EntitySet element.

备注

此处未列出 (一些属性) 可以用 存储 别名进行限定。Some attributes (not listed here) may be qualified with the store alias. 在更新模型时,模型更新向导会使用这些特性。These attributes are used by the Update Model Wizard when updating a model.

属性名称Attribute Name 是否必需Is Required Value
名称Name Yes 实体集的名称。The name of the entity set.
EntityTypeEntityType Yes 实体集包含其实例的实体类型的完全限定名称。The fully-qualified name of the entity type for which the entity set contains instances.
架构Schema No 数据库架构。The database schema.
Table No 数据库表。The database table.

备注

可以将任意数量的批注属性 (自定义 XML 特性) 应用于 EntitySet 元素。Any number of annotation attributes (custom XML attributes) may be applied to the EntitySet element. 然而,自定义特性可能不属于为 SSDL 保留的任何 XML 命名空间。However, custom attributes may not belong to any XML namespace that is reserved for SSDL. 任何两个自定义特性的完全限定名称都不能相同。The fully-qualified names for any two custom attributes cannot be the same.

示例Example

下面的示例演示一个具有两个EntitySet元素和一个AssociationSet元素的EntityContainer元素:The following example shows an EntityContainer element that has two EntitySet elements and one AssociationSet element:

 <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)EntityType Element (SSDL)

存储架构定义语言 (SSDL) 中的 EntityType 元素表示基础数据库的表或视图中的行。An EntityType element in store schema definition language (SSDL) represents a row in a table or view of the underlying database. 以 SSDL 表示的 EntitySet 元素表示行所在的表或视图。An EntitySet element in SSDL represents the table or view in which rows occur. EntitySet元素的EntityType特性指定了表示 ssdl 实体集中的行的特定 SSDL 实体类型。The EntityType attribute of an EntitySet element specifies the particular SSDL entity type that represents rows in an SSDL entity set. SSDL 实体类型与 CSDL 实体类型之间的映射在 EntityTypeMapping 元素中指定。The mapping between an SSDL entity type and a CSDL entity type is specified in an EntityTypeMapping element.

EntityType元素可以具有以下子元素 (按列出的顺序) :The EntityType element can have the following child elements (in the order listed):

  • Documentation(零个或一个元素)Documentation (zero or one element)
  • Key(零个或一个元素)Key (zero or one element)
  • 批注元素Annotation elements

适用的属性Applicable Attributes

下表介绍可应用于 EntityType 元素的特性。The table below describes the attributes that can be applied to the EntityType element.

属性名称Attribute Name 是否必需Is Required Value
名称Name Yes 实体类型的名称。The name of the entity type. 此值通常与以实体类型表示行的表的名称相同。This value is usually the same as the name of the table in which the entity type represents a row. 此值可以不包含句点 (.)。This value can contain no periods (.).

备注

可以将任意数量的批注属性 (自定义 XML 特性) 应用于 EntityType 元素。Any number of annotation attributes (custom XML attributes) may be applied to the EntityType element. 然而,自定义特性可能不属于为 SSDL 保留的任何 XML 命名空间。However, custom attributes may not belong to any XML namespace that is reserved for SSDL. 任何两个自定义特性的完全限定名称都不能相同。The fully-qualified names for any two custom attributes cannot be the same.

示例Example

下面的示例演示具有两个属性的 EntityType 元素:The following example shows an EntityType element with two properties:

 <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)Function Element (SSDL)

存储架构定义语言 (SSDL) 中的 Function 元素指定基础数据库中存在的存储过程。The Function element in store schema definition language (SSDL) specifies a stored procedure that exists in the underlying database.

函数元素可以具有以下子元素 (按列出的顺序) :The Function element can have the following child elements (in the order listed):

  • Documentation(零个或一个)Documentation (zero or one)
  • 参数 (零个或多个) Parameter (zero or more)
  • CommandText (零个或一个) CommandText (zero or one)
  • ReturnType (零个或多个) ReturnType (zero or more)
  • 批注元素(零个或多个)Annotation elements (zero or more)

函数的返回类型必须与 returntype 元素或 returntype 属性一起指定 (参见下面) ,但不能同时指定两者。A return type for a function must be specified with either the ReturnType element or the ReturnType attribute (see below), but not both.

可以将在存储模型中指定的存储过程导入应用程序的概念模型。Stored procedures that are specified in the storage model can be imported into the conceptual model of an application. 有关详细信息,请参阅 用存储过程查询For more information, see Querying with Stored Procedures. 函数元素还可用于在存储模型中定义自定义函数。The Function element can also be used to define custom functions in the storage model.  

适用的属性Applicable Attributes

下表介绍可应用于 Function 元素的特性。The following table describes the attributes that can be applied to the Function element.

备注

此处未列出 (一些属性) 可以用 存储 别名进行限定。Some attributes (not listed here) may be qualified with the store alias. 在更新模型时,模型更新向导会使用这些特性。These attributes are used by the Update Model Wizard when updating a model.

属性名称Attribute Name 是否必需Is Required Value
名称Name Yes 存储过程的名称。The name of the stored procedure.
ReturnTypeReturnType No 存储过程的返回类型。The return type of the stored procedure.
聚合Aggregate No 如果存储过程返回聚合值,则为 True ;否则为 FalseTrue if the stored procedure returns an aggregate value; otherwise False.
LBuiltIn No 如果函数是内置的1函数,则为True ; 否则为。否则为 FalseTrue if the function is a built-in1 function; otherwise False.
StoreFunctionNameStoreFunctionName No 存储过程的名称。The name of the stored procedure.
NiladicFunctionNiladicFunction No 如果函数是 niladic2函数,则为True ;否则为 FalseTrue if the function is a niladic2 function; False otherwise.
IsComposableIsComposable No 如果函数是可组合的3函数,则为True ;否则为 FalseTrue if the function is a composable3 function; False otherwise.
ParameterTypeSemanticsParameterTypeSemantics No 定义用于解析函数重载的类型语义的枚举。The enumeration that defines the type semantics used to resolve function overloads. 该枚举是在提供程序清单中根据函数定义来定义的。The enumeration is defined in the provider manifest per function definition. 默认值为 AllowImplicitConversionThe default value is AllowImplicitConversion.
架构Schema No 在其中定义存储过程的架构的名称。The name of the schema in which the stored procedure is defined.

1 内置函数是在数据库中定义的函数。1 A built-in function is a function that is defined in the database. 有关在存储模型中定义的函数的信息,请参阅 CommandText 元素 (SSDL) 。For information about functions that are defined in the storage model, see CommandText Element (SSDL).

2 niladic 函数是不接受任何参数的函数,并且在调用时不需要括号。2 A niladic function is a function that accepts no parameters and, when called, does not require parentheses.

3 如果一个函数的输出可以是另一个函数的输入,则这两个函数是可组合的。3 Two functions are composable if the output of one function can be the input for the other function.

备注

可以将任意数量的批注属性 (自定义 XML 特性) 可以应用于 Function 元素。Any number of annotation attributes (custom XML attributes) may be applied to the Function element. 然而,自定义特性可能不属于为 SSDL 保留的任何 XML 命名空间。However, custom attributes may not belong to any XML namespace that is reserved for SSDL. 任何两个自定义特性的完全限定名称都不能相同。The fully-qualified names for any two custom attributes cannot be the same.

示例Example

下面的示例显示了与UpdateOrderQuantity存储过程对应的Function元素。The following example shows a Function element that corresponds to the UpdateOrderQuantity stored procedure. 该存储过程接受两个参数,且不返回值。The stored procedure accepts two parameters and does not return a value.

 <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 Element (SSDL)

存储架构定义语言中 (SSDL) 的 Key 元素表示基础数据库中的表的主键。The Key element in store schema definition language (SSDL) represents the primary key of a table in the underlying database. Key 是 EntityType 元素的子元素,它表示表中的行。Key is a child element of an EntityType element, which represents a row in a table. 通过引用EntityType元素中定义的一个或多个属性元素,在key元素中定义主键。The primary key is defined in the Key element by referencing one or more Property elements that are defined on the EntityType element.

Key元素可以具有以下子元素 (按列出的顺序) :The Key element can have the following child elements (in the order listed):

  • PropertyRef(一个或多个)PropertyRef (one or more)
  • 批注元素Annotation elements

没有任何特性适用于 该键 元素。No attributes are applicable to the Key element.

示例Example

下面的示例演示一个具有引用一个属性的键的 EntityType 元素:The following example shows an EntityType element with a key that references one property:

 <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)OnDelete Element (SSDL)

(SSDL) 存储架构定义语言中的 OnDelete 元素反映了参与外键约束的行被删除时的数据库行为。The OnDelete element in store schema definition language (SSDL) reflects the database behavior when a row that participates in a foreign key constraint is deleted. 如果将操作设置为 Cascade,则还将删除引用要删除的行的行。If the action is set to Cascade, then rows that reference a row that is being deleted will also be deleted. 如果将操作设置为 " ",则不会同时删除引用要删除行的行。If the action is set to None, then rows that reference a row that is being deleted are not also deleted. OnDelete元素是结束元素的子元素。An OnDelete element is a child element of an End element.

OnDelete元素可以具有以下子元素 (按列出的顺序) :An OnDelete element can have the following child elements (in the order listed):

  • Documentation(零个或一个)Documentation (zero or one)
  • 批注元素(零个或多个)Annotation elements (zero or more)

适用的属性Applicable Attributes

下表介绍可应用于 OnDelete 元素的特性。The following table describes the attributes that can be applied to the OnDelete element.

属性名称Attribute Name 是否必需Is Required “值”Value
操作Action Yes CascadeNoneCascade or None. (限制 值有效,但行为与 None相同。 ) (The value Restricted is valid but has the same behavior as None.)

备注

可以将任意数量的批注属性 (自定义 XML 特性) 应用到 OnDelete 元素。Any number of annotation attributes (custom XML attributes) may be applied to the OnDelete element. 然而,自定义特性可能不属于为 SSDL 保留的任何 XML 命名空间。However, custom attributes may not belong to any XML namespace that is reserved for SSDL. 任何两个自定义特性的完全限定名称都不能相同。The fully-qualified names for any two custom attributes cannot be the same.

示例Example

下面的示例演示了一个定义FK _ CustomerOrders外键约束的Association元素。The following example shows an Association element that defines the FK_CustomerOrders foreign key constraint. OnDelete元素指示在删除customers表中的某一行时,该订单表中引用该特定行的所有行都将被删除。The OnDelete element indicates that all rows in the Orders table that reference a particular row in the Customers table will be deleted if the row in the Customers table is deleted.

 <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)Parameter Element (SSDL)

存储架构定义语言 (SSDL) 中的 Parameter 元素是 Function 元素的子元素,用于指定数据库中存储过程的参数。The Parameter element in store schema definition language (SSDL) is a child of the Function element that specifies parameters for a stored procedure in the database.

Parameter元素可以具有以下子元素 (按列出的顺序) :The Parameter element can have the following child elements (in the order listed):

  • Documentation(零个或一个)Documentation (zero or one)
  • 批注元素(零个或多个)Annotation elements (zero or more)

适用的属性Applicable Attributes

下表描述了可应用于 参数 元素的特性。The table below describes the attributes that can be applied to the Parameter element.

属性名称Attribute Name 是否必需Is Required Value
名称Name Yes 参数的名称。The name of the parameter.
类型Type Yes 参数类型。The parameter type.
模式Mode No InOutInOut ,具体取决于参数是输入、输出还是输入/输出参数。In, Out, or InOut depending on whether the parameter is an input, output, or input/output parameter.
MaxLengthMaxLength No 参数的最大长度。The maximum length of the parameter.
精度Precision No 参数的精度。The precision of the parameter.
缩放Scale No 参数的确定位数。The scale of the parameter.
SRID SRID No 空间系统引用标识符。Spatial System Reference Identifier. 仅对空间类型的参数有效。Valid only for parameters of spatial types. 有关详细信息,请参阅 SRID and SRID (SQL Server) For more information, see SRID and SRID (SQL Server).

备注

可以将任意数量的批注属性 (自定义 XML 特性) 可应用于 参数 元素。Any number of annotation attributes (custom XML attributes) may be applied to the Parameter element. 然而,自定义特性可能不属于为 SSDL 保留的任何 XML 命名空间。However, custom attributes may not belong to any XML namespace that is reserved for SSDL. 任何两个自定义特性的完全限定名称都不能相同。The fully-qualified names for any two custom attributes cannot be the same.

示例Example

下面的示例演示一个具有两个参数元素的 函数 元素,这些 参数 元素指定输入参数:The following example shows a Function element that has two Parameter elements that specify input parameters:

 <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 Element (SSDL)

存储架构定义语言 (SSDL) 中的 主体 元素是 ReferentialConstraint 元素的子元素,用于定义外键约束 (也称为引用约束) 的主体端。The Principal element in store schema definition language (SSDL) is a child element to the ReferentialConstraint element that defines the principal end of a foreign key constraint (also called a referential constraint). 主体元素指定主键列 (或列) 在另一个列 (或列) 引用的表中。The Principal element specifies the primary key column (or columns) in a table that is referenced by another column (or columns). PropertyRef 元素指定要引用的列。PropertyRef elements specify which columns are referenced. Dependent 元素指定引用 主体 元素中指定的主键列的列。The Dependent element specifies columns that reference the primary key columns that are specified in the Principal element.

主体元素可以具有以下子元素 (按列出的顺序) :The Principal element can have the following child elements (in the order listed):

  • PropertyRef(一个或多个)PropertyRef (one or more)
  • 批注元素(零个或多个)Annotation elements (zero or more)

适用的属性Applicable Attributes

下表介绍可应用于 主体 元素的特性。The following table describes the attributes that can be applied to the Principal element.

属性名称Attribute Name 是否必需Is Required “值”Value
角色Role Yes Role 特性相同的值 (如果使用) 对应的结束元素,则为; 否则为。否则,为包含被引用列的表的名称。The same value as the Role attribute (if used) of the corresponding End element; otherwise, the name of the table that contains the referenced column.

备注

可以将任意数量的批注属性 (自定义 XML 特性) 应用于 主体 元素。Any number of annotation attributes (custom XML attributes) may be applied to the Principal element. 然而,自定义特性可能不属于为 CSDL 保留的任何 XML 命名空间。However, custom attributes may not belong to any XML namespace that is reserved for CSDL. 任何两个自定义特性的完全限定名称都不能相同。The fully-qualified names for any two custom attributes cannot be the same.

示例Example

下面的示例演示一个 Association 元素,该元素使用 ReferentialConstraint 元素指定参与 FK _ CustomerOrders 外键约束的列。The following example shows an Association element that uses a ReferentialConstraint element to specify the columns that participate in the FK_CustomerOrders foreign key constraint. 主体元素将Customer表的CustomerId列指定为约束的主体端。The Principal element specifies the CustomerId column of the Customer table as the principal end of the constraint.

 <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 Element (SSDL)

存储架构定义语言 (SSDL) 中的 属性 元素表示基础数据库中的表中的列。The Property element in store schema definition language (SSDL) represents a column in a table in the underlying database. 属性 元素是 EntityType 元素的子元素,表示表中的行。Property elements are children of EntityType elements, which represent rows in a table. EntityType元素上定义的每个属性元素都表示一列。Each Property element defined on an EntityType element represents a column.

属性元素不能具有任何子元素。A Property element cannot have any child elements.

适用的属性Applicable Attributes

下表介绍可应用于 属性 元素的特性。The following table describes the attributes that can be applied to the Property element.

属性名称Attribute Name 是否必需Is Required Value
名称Name Yes 对应列的名称。The name of the corresponding column.
类型Type Yes 对应列的类型。The type of the corresponding column.
可以为 NullNullable No (默认值为 True) 或False ,具体取决于对应列是否可以具有 null 值。True (the default value) or False depending on whether the corresponding column can have a null value.
DefaultValueDefaultValue No 对应列的默认值。The default value of the corresponding column.
MaxLengthMaxLength No 对应列的最大长度。The maximum length of the corresponding column.
FixedLengthFixedLength No TrueFalse ,具体取决于相应的列值是否将作为固定长度字符串存储。True or False depending on whether the corresponding column value will be stored as a fixed length string.
精度Precision No 对应列的精度。The precision of the corresponding column.
缩放Scale No 对应列的小数位数。The scale of the corresponding column.
UnicodeUnicode No TrueFalse ,具体取决于对应列值是否将存储为 Unicode 字符串。True or False depending on whether the corresponding column value will be stored as a Unicode string.
排序规则Collation No 指定要在数据源中使用的排序的字符串。A string that specifies the collating sequence to be used in the data source.
SRID SRID No 空间系统引用标识符。Spatial System Reference Identifier. 仅对空间类型的属性有效。Valid only for properties of spatial types. 有关详细信息,请参阅 SRID and SRID (SQL Server) For more information, see SRID and SRID (SQL Server).
StoreGeneratedPatternStoreGeneratedPattern No 标识 (如果相应的列值是在数据库) 中生成的标识,则为; 如果在数据库) 中计算相应列值,则为 计算 (。None, Identity (if the corresponding column value is an identity that is generated in the database), or Computed (if the corresponding column value is computed in the database). 对于 RowType 属性无效。Not Valid for RowType properties.

备注

可以将任意数量的批注属性 (自定义 XML 特性) 可以应用于 Property 元素。Any number of annotation attributes (custom XML attributes) may be applied to the Property element. 然而,自定义特性可能不属于为 SSDL 保留的任何 XML 命名空间。However, custom attributes may not belong to any XML namespace that is reserved for SSDL. 任何两个自定义特性的完全限定名称都不能相同。The fully-qualified names for any two custom attributes cannot be the same.

示例Example

下面的示例演示一个具有两个子属性元素的EntityType元素:The following example shows an EntityType element with two child Property elements:

 <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)PropertyRef Element (SSDL)

存储架构定义语言 (SSDL) 中的 PropertyRef 元素引用 EntityType 元素上定义的属性,以指示该属性将执行以下角色之一:The PropertyRef element in store schema definition language (SSDL) references a property defined on an EntityType element to indicate that the property will perform one of the following roles:

  • EntityType 表示的表的主键的一部分。Be part of the primary key of the table that the EntityType represents. 一个或多个 PropertyRef 元素可用于定义主键。One or more PropertyRef elements can be used to define a primary key. 有关更多信息,请参见 Key 元素。For more information, see Key element.
  • 是引用约束的依赖端或主体端。Be the dependent or principal end of a referential constraint. 有关更多信息,请参见 ReferentialConstraint 元素。For more information, see ReferentialConstraint element.

PropertyRef元素只能具有以下子元素:The PropertyRef element can only have the following child elements:

  • Documentation(零个或一个)Documentation (zero or one)
  • 批注元素Annotation elements

适用的属性Applicable Attributes

下表介绍可应用于 PropertyRef 元素的特性。The table below describes the attributes that can be applied to the PropertyRef element.

属性名称Attribute Name 是否必需Is Required Value
名称Name Yes 所引用属性的名称。The name of the referenced property.

备注

可以将任意数量的批注属性 (自定义 XML 特性) 应用到 PropertyRef 元素。Any number of annotation attributes (custom XML attributes) may be applied to the PropertyRef element. 然而,自定义特性可能不属于为 CSDL 保留的任何 XML 命名空间。However, custom attributes may not belong to any XML namespace that is reserved for CSDL. 任何两个自定义特性的完全限定名称都不能相同。The fully-qualified names for any two custom attributes cannot be the same.

示例Example

下面的示例演示了一个 PropertyRef 元素,该元素用于通过引用 EntityType 元素上定义的属性来定义主键。The following example shows a PropertyRef element used to define a primary key by referencing a property that is defined on an EntityType element.

 <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 Element (SSDL)

存储架构定义语言 (SSDL) 中的 ReferentialConstraint 元素表示一个外键约束, (在基础数据库中也称为 "引用完整性) 约束"。The ReferentialConstraint element in store schema definition language (SSDL) represents a foreign key constraint (also called a referential integrity constraint) in the underlying database. 约束的主体端和依赖端分别由 Principal 和 Dependent 子元素指定。The principal and dependent ends of the constraint are specified by the Principal and Dependent child elements, respectively. 通过 PropertyRef 元素引用参与主体端和依赖端的列。Columns that participate in the principal and dependent ends are referenced with PropertyRef elements.

ReferentialConstraint元素是 Association 元素的可选子元素。The ReferentialConstraint element is an optional child element of the Association element. 如果未使用 ReferentialConstraint 元素映射 Association 元素中指定的 foreign key 约束,则必须使用 AssociationSetMapping 元素来执行此操作。If a ReferentialConstraint element is not used to map the foreign key constraint that is specified in the Association element, an AssociationSetMapping element must be used to do this.

ReferentialConstraint元素可以具有以下子元素:The ReferentialConstraint element can have the following child elements:

  • Documentation(零个或一个)Documentation (zero or one)
  • Principal(恰好一个)Principal (exactly one)
  • Dependent(恰好一个)Dependent (exactly one)
  • 批注元素(零个或多个)Annotation elements (zero or more)

适用的属性Applicable Attributes

可以将任意数量的批注属性 (自定义 XML 特性) 应用到 ReferentialConstraint 元素。Any number of annotation attributes (custom XML attributes) may be applied to the ReferentialConstraint element. 然而,自定义特性可能不属于为 SSDL 保留的任何 XML 命名空间。However, custom attributes may not belong to any XML namespace that is reserved for SSDL. 任何两个自定义特性的完全限定名称都不能相同。The fully-qualified names for any two custom attributes cannot be the same.

示例Example

下面的示例演示一个 Association 元素,该元素使用 ReferentialConstraint 元素指定参与 FK _ CustomerOrders 外键约束的列:The following example shows an Association element that uses a ReferentialConstraint element to specify the columns that participate in the FK_CustomerOrders foreign key constraint:

 <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) ReturnType Element (SSDL)

存储架构定义语言中的 ReturnType 元素 (SSDL) 指定 函数 元素中定义的函数的返回类型。The ReturnType element in store schema definition language (SSDL) specifies the return type for a function that is defined in a Function element. 还可以使用 ReturnType 特性指定函数返回类型。A function return type can also be specified with a ReturnType attribute.

函数的返回类型是使用 type 特性或 ReturnType 元素指定的。The return type of a function is specified with the Type attribute or the ReturnType element.

ReturnType元素可以具有以下子元素:The ReturnType element can have the following child elements:

  • CollectionType (一个) CollectionType (one)

备注

可以将任意数量的批注属性 (自定义 XML 特性) 可应用于 ReturnType 元素。Any number of annotation attributes (custom XML attributes) may be applied to the ReturnType element. 然而,自定义特性可能不属于为 SSDL 保留的任何 XML 命名空间。However, custom attributes may not belong to any XML namespace that is reserved for SSDL. 任何两个自定义特性的完全限定名称都不能相同。The fully-qualified names for any two custom attributes cannot be the same.

示例Example

下面的示例使用一个 函数 ,该函数返回行的集合。The following example uses a Function that returns a collection of rows.

   <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>

(SSDL) 的 RowType 元素RowType Element (SSDL)

存储架构定义语言 (SSDL) 中的 RowType 元素将未命名的结构定义为在存储区中定义的函数的返回类型。A RowType element in store schema definition language (SSDL) defines an unnamed structure as a return type for a function defined in the store.

RowType元素是CollectionType元素的子元素:A RowType element is the child element of CollectionType element:

RowType元素可以具有以下子元素:A RowType element can have the following child elements:

  • Property(一个或多个)Property (one or more)

示例Example

下面的示例演示了一个存储函数,该函数使用 CollectionType 元素来指定函数返回 (在 RowType 元素) 中指定的行的集合。The following example shows a store function that uses a CollectionType element to specify that the function returns a collection of rows (as specified in the RowType element).

   <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 Element (SSDL)

存储架构定义语言 (SSDL) 中的 架构 元素是存储模型定义的根元素。The Schema element in store schema definition language (SSDL) is the root element of a storage model definition. 它包括构成存储模型的对象、函数和容器的定义。It contains definitions for the objects, functions, and containers that make up a storage model.

Schema元素可包含零个或多个下列子元素:The Schema element may contain zero or more of the following child elements:

  • 关联Association
  • EntityTypeEntityType
  • EntityContainerEntityContainer
  • 函数Function

Schema元素使用namespace属性为存储模型中的实体类型和关联对象定义命名空间。The Schema element uses the Namespace attribute to define the namespace for the entity type and association objects in a storage model. 在命名空间内,任何两个对象都不能同名。Within a namespace, no two objects can have the same name.

存储模型命名空间不同于 Schema 元素的 XML 命名空间。A storage model namespace is different from the XML namespace of the Schema element. 命名空间特性定义的存储模型命名空间 () 是实体类型和关联类型的逻辑容器。A storage model namespace (as defined by the Namespace attribute) is a logical container for entity types and association types. Schema元素的xmlns特性) 指示 (XML 命名空间是schema元素的子元素和属性的默认命名空间。The XML namespace (indicated by the xmlns attribute) of a Schema element is the default namespace for child elements and attributes of the Schema element. 格式为 (的 XML 命名空间 https://schemas.microsoft.com/ado/YYYY/MM/edm/ssdl (YYYY 和 MM 分别表示年份和月份)) 为 SSDL 预留。XML namespaces of the form https://schemas.microsoft.com/ado/YYYY/MM/edm/ssdl (where YYYY and MM represent a year and month respectively) are reserved for SSDL. 自定义元素和特性不能位于具有此格式的命名空间中。Custom elements and attributes cannot be in namespaces that have this form.

适用的属性Applicable Attributes

下表描述了可对 Schema 元素应用的属性。The table below describes the attributes can be applied to the Schema element.

属性名称Attribute Name 是否必需Is Required Value
NamespaceNamespace Yes 存储模型的命名空间。The namespace of the storage model. 命名空间属性的值用于构成类型的完全限定名称。The value of the Namespace attribute is used to form the fully qualified name of a type. 例如,如果名为Customerentitytype在位于 examplemodel.store 命名空间中,则entitytype的完全限定名称为位于 examplemodel.store。For example, if an EntityType named Customer is in the ExampleModel.Store namespace, then the fully qualified name of the EntityType is ExampleModel.Store.Customer.
以下字符串不能用作 Namespace 特性的值: System暂时性EdmThe following strings cannot be used as the value for the Namespace attribute: System, Transient, or Edm. Namespace属性的值不能与 CSDL Schema 元素中namespace属性的值相同。The value for the Namespace attribute cannot be the same as the value for the Namespace attribute in the CSDL Schema element.
AliasAlias No 用于取代命名空间名称的标识符。An identifier used in place of the namespace name. 例如,如果名为CustomerEntityType位于位于 examplemodel.store 命名空间中,并且Alias属性的值为Storagemodel.customer,则可以使用 storagemodel.customer 作为 EntityType 的完全限定名称 For example, if an EntityType named Customer is in the ExampleModel.Store namespace and the value of the Alias attribute is StorageModel, then you can use StorageModel.Customer as the fully qualified name of the EntityType.
提供程序Provider Yes 数据提供程序。The data provider.
ProviderManifestTokenProviderManifestToken Yes 一个标记,该标记指示提供程序清单返回到的提供程序。A token that indicates to the provider which provider manifest to return. 没有为该标记定义格式。No format for the token is defined. 标记的值由提供程序定义。Values for the token are defined by the provider. 有关 SQL Server 提供程序清单令牌的信息,请参阅 SqlClient for 实体框架。For information about SQL Server provider manifest tokens, see SqlClient for Entity Framework.

示例Example

下面的示例演示一个 架构 元素,该元素包含一个 EntityContainer 元素、两个 EntityType 元素和一个 Association 元素。The following example shows a Schema element that contains an EntityContainer element, two EntityType elements, and one Association element.

 <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>

Annotation 特性Annotation Attributes

以存储架构定义语言 (SSDL) 表示的批注特性在存储模型中是自定义 XML 特性,这些特性提供有关存储模型中元素的额外元数据。Annotation attributes in store schema definition language (SSDL) are custom XML attributes in the storage model that provide extra metadata about the elements in the storage model. 除了具有有效的 XML 结构之外,以下约束也适用于批注特性:In addition to having valid XML structure, the following constraints apply to annotation attributes:

  • 批注特性不能位于为 SSDL 保留的任何 XML 命名空间中。Annotation attributes must not be in any XML namespace that is reserved for SSDL.
  • 任何两个批注特性的完全限定名称都不能相同。The fully-qualified names of any two annotation attributes must not be the same.

可以将多个批注特性应用于一个给定的 SSDL 元素。More than one annotation attribute may be applied to a given SSDL element. 可以在运行时通过使用 System.web 命名空间中的类访问批注元素中包含的元数据。Metadata contained in annotation elements can be accessed at runtime by using classes in the System.Data.Metadata.Edm namespace.

示例Example

下面的示例演示一个 EntityType 元素,该元素具有应用于 " 订单 id " 属性的批注特性。The following example shows an EntityType element that has an annotation attribute applied to the OrderId property. 该示例还显示了添加到 EntityType 元素的批注元素。The example also show an annotation element added to the EntityType element.

 <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>

批注元素 (SSDL)Annotation Elements (SSDL)

以存储架构定义语言 (SSDL) 表示的批注元素是存储模型中的自定义 XML 元素,可提供有关存储模型的额外元数据。Annotation elements in store schema definition language (SSDL) are custom XML elements in the storage model that provide extra metadata about the storage model. 除了具有有效的 XML 结构之外,批注元素还应满足以下约束:In addition to having valid XML structure, the following constraints apply to annotation elements:

  • 批注元素不能位于为 SSDL 保留的任何 XML 命名空间中。Annotation elements must not be in any XML namespace that is reserved for SSDL.
  • 任何两个批注元素的完全限定名称都不能相同。The fully-qualified names of any two annotation elements must not be the same.
  • 批注元素必须出现在给定 SSDL 元素的所有其他子元素之后。Annotation elements must appear after all other child elements of a given SSDL element.

多个批注元素可能是某个给定 SSDL 元素的子元素。More than one annotation element may be a child of a given SSDL element. 从 .NET Framework 版本4开始,可以在运行时通过使用 System.web 命名空间中的类访问批注元素中包含的元数据。Starting with the .NET Framework version 4, metadata contained in annotation elements can be accessed at runtime by using classes in the System.Data.Metadata.Edm namespace.

示例Example

下面的示例演示一个 EntityType 元素,该元素具有一个 (CustomElement) 的批注元素。The following example shows an EntityType element that has an annotation element (CustomElement). 该示例还显示了应用于 " 订单 id " 属性的批注特性。The example also shows an annotation attribute applied to the OrderId property.

 <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>

方面 (SSDL)Facets (SSDL)

以存储架构定义语言 (SSDL) 表示的方面表示对于 Property 元素中指定的列类型的约束。Facets in store schema definition language (SSDL) represent constraints on column types that are specified in Property elements. Facet 作为 属性 元素上的 XML 特性实现。Facets are implemented as XML attributes on Property elements.

下表描述了 SSDL 中支持的方面:The following table describes the facets that are supported in SSDL:

方面Facet 说明Description
排序规则Collation 指定在对属性值执行比较和排序操作时要使用的排序序列。Specifies the collating sequence (or sorting sequence) to be used when performing comparison and ordering operations on values of the property.
FixedLengthFixedLength 指定列值的长度是否可变。Specifies whether the length of the column value can vary.
MaxLengthMaxLength 指定列值的最大长度。Specifies the maximum length of the column value.
精度Precision 对于 Decimal类型的属性,指定属性值可以具有的位数。For properties of type Decimal, specifies the number of digits a property value can have. 对于类型为 TimeDateTimeDateTimeOffset的属性,指定列值的秒小数部分的位数。For properties of type Time, DateTime, and DateTimeOffset, specifies the number of digits for the fractional part of seconds of the column value.
缩放Scale 指定列值小数点右侧的位数。Specifies the number of digits to the right of the decimal point for the column value.
UnicodeUnicode 指示是否将列值存储为 Unicode。Indicates whether the column value is stored as Unicode.