实体数据模型 (EDM) 中的复杂类型是具有内部结构但没有键属性的数据类型。复杂类型用于实现具有其自己的内部属性的属性。实体或复杂类型可能具有声明为复杂类型的属性。
复杂类型以类似于实体类型的语法声明。主要差别在于复杂类型不包括键属性的声明,但对于实体则包括此声明。在映射规范中,复杂类型映射为复杂属性。
实现复杂属性
-
在 Visual Studio 中创建类库类型的项目,然后添加对 System.Data.Entity 和 System.Runtime.Serialization 的引用。
-
向项目中添加新的实体数据模型模板。
-
实现概念架构设计语言 (CSDL) 架构并将其添加到由 EDM 模板提供的 CSDL 文件中。此架构包含 CCustomer 实体及其使用复杂类型 CAddress 的 Address 属性的声明。以下示例代码中显示了完整的架构。
-
对第 2 步中创建的概念架构运行 edmgen.exe 以创建对象代码。
-
实现存储架构定义语言 (SSDL) 架构并将其添加到由 EDM 模板提供的 SSDL 文件中。在此架构中,复杂类型数据与包含实体存储在同一个表中,如以下存储架构设计语言 (SSDL) 元数据中所示。以下示例代码中显示了完整的架构。
-
实现映射规范语言 (MSL) 架构并将其添加到由 EDM 模板提供的 MSL 文件中。以下映射架构将 Address 的映射显示为复杂属性。以下示例代码中显示了完整的映射规范。
示例
以下 XML 中连续显示了三个所需的架构。在该代码之后提供了一个脚本,可以用于创建此示例中用于存储的数据库。
<Schema Namespace="CustomerComplexAddress" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm"> <EntityContainer Name="CustomerComplexAddressContext"> <EntitySet Name="CCustomers" EntityType="CustomerComplexAddress.CCustomer" /> </EntityContainer> <EntityType Name="CCustomer"> <Key> <PropertyRef Name="CustomerId" /> </Key> <Property Name="CustomerId" Type="Int32" Nullable="false" /> <Property Name="CompanyName" Type="String" /> <Property Name="ContactName" Type="String" /> <Property Name="ContactTitle" Type="String" /> <Property Name="Address" Type="Self.CAddress" Nullable="false" /> </EntityType> <ComplexType Name="CAddress"> <Property Name="StreetAddress" Type="String" /> <Property Name="City" Type="String" /> <Property Name="Region" Type="String" /> <Property Name="PostalCode" Type="String" /> <Property Name="Country" Type="String" /> <Property Name="Phone" Type="String" /> <Property Name="Fax" Type="String" /> </ComplexType> </Schema> <Schema Namespace="CustomerComplexAddress.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl"> <EntityContainer Name="dbo"> <EntitySet Name="SCustomer" EntityType="CustomerComplexAddress.Store.SCustomer" /> </EntityContainer> <EntityType Name="SCustomer"> <Key> <PropertyRef Name="CustomerId" /> </Key> <Property Name="CustomerId" Type="int" Nullable="false" /> <Property Name="CompanyName" Type="nvarchar" MaxLength="50" /> <Property Name="ContactName" Type="nvarchar" MaxLength="50" /> <Property Name="ContactTitle" Type="nvarchar" MaxLength="50" /> <Property Name="Address" Type="nvarchar" MaxLength="50" /> <Property Name="City" Type="nvarchar" MaxLength="50" /> <Property Name="Region" Type="nvarchar" MaxLength="50" /> <Property Name="PostalCode" Type="nvarchar" MaxLength="50" /> <Property Name="Country" Type="nvarchar" MaxLength="50" /> <Property Name="Phone" Type="nvarchar" MaxLength="50" /> <Property Name="Fax" Type="nvarchar" MaxLength="50" /> </EntityType> </Schema> <Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS"> <EntityContainerMapping StorageEntityContainer="dbo" CdmEntityContainer="CustomerComplexAddressContext"> <EntitySetMapping Name="CCustomers"> <EntityTypeMapping TypeName="CustomerComplexAddress.CCustomer"> <MappingFragment StoreEntitySet="SCustomer"> <ScalarProperty Name="CustomerId" ColumnName="CustomerId" /> <ScalarProperty Name="CompanyName" ColumnName="CompanyName" /> <ScalarProperty Name="ContactName" ColumnName="ContactName" /> <ScalarProperty Name="ContactTitle" ColumnName="ContactTitle" /> <ComplexProperty Name="Address" TypeName="CustomerComplexAddress.CAddress"> <ScalarProperty Name="StreetAddress" ColumnName="Address" /> <ScalarProperty Name="City" ColumnName="City" /> <ScalarProperty Name="Region" ColumnName="Region" /> <ScalarProperty Name="PostalCode" ColumnName="PostalCode" /> <ScalarProperty Name="Country" ColumnName="Country" /> <ScalarProperty Name="Phone" ColumnName="Phone" /> <ScalarProperty Name="Fax" ColumnName="Fax" /> </ComplexProperty> </MappingFragment> </EntityTypeMapping> </EntitySetMapping> </EntityContainerMapping> </Mapping> The following script can be used to create SQL Server storage model. USE [master] GO CREATE DATABASE [CustomerComplexAddress] GO USE [CustomerComplexAddress] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SCustomer]') AND type in (N'U')) BEGIN CREATE TABLE [dbo].[SCustomer]( [CustomerId] [int] NOT NULL, [CompanyName] [nvarchar](50) NULL, [ContactName] [nvarchar](50) NULL, [ContactTitle] [nvarchar](50) NULL, [Address] [nvarchar](50) NULL, [City] [nvarchar](50) NULL, [Region] [nvarchar](50) NULL, [PostalCode] [nvarchar](50) NULL, [Country] [nvarchar](50) NULL, [Phone] [nvarchar](50) NULL, [Fax] [nvarchar](50) NULL, CONSTRAINT [PK_SCustomer] PRIMARY KEY CLUSTERED ( [CustomerId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] END GOhttp://msdn.microsoft.com/zh-cn/library/bb738613(v=vs.90).aspx