共用方式為


HOW TO:使用每個類型的多重實體來定義模型 (Entity Framework)

Entity Data Model (EDM) 允許某個實體類型包含在單一實體容器內部的多個實體集中,或允許某個實體類型包含在多個實體容器內部的實體集中。當資料庫具有多個資料表有相同結構的分割或其他這類案例時,定義每個類型的多重實體 (MEST) 可讓使用者簡化程式碼。如需詳細資訊,請參閱實體集 (EDM)

若要實作 MEST 資料模型的概念結構描述

  1. 建立類別庫 (Class Library) 專案並加入新的 EDM 範本。

  2. 針對東區 CustomersEast 和西區 CustomersWest 的客戶,實作包含在兩個實體集中的 Customer 實體類型。

  3. 請注意兩個地區客戶實體集中所使用之單一 Customer 實體類型與 OrdersEastOrdersWest 實體集中之兩種訂單類型 OrderEastOrderWest 之間的差異。

  4. 實作單一 Customer 類型與兩種地區訂單類型 OrderEastOrderWest 之間的關聯,以便反映 MEST 結構。Customer 類型與地區訂單類型之間的兩種關聯都會在基數為一的關聯端上指定 Customer

  5. 實作在概念結構定義語言 (CSDL) 結構描述中包含 Customer 類型的兩個實體集。實體集 CustomersEast 和實體集 CustomersWest 都會指定 RegionalCustomersModel.Customer 當做其實體類型。

<?xml version="1.0" encoding="utf-8"?>
<Schema Namespace="RegionalCustomersModel"
              Alias="Self"
              xmlns="https://schemas.microsoft.com/ado/2006/04/edm">

        <EntityContainer Name="RegionalCustomersEntities">

          <EntitySet Name="CustomersEast" 
                     EntityType="RegionalCustomersModel.Customer" />
          <EntitySet Name="CustomersWest" 
                     EntityType="RegionalCustomersModel.Customer" />
          <EntitySet Name="OrdersEast" 
                     EntityType="RegionalCustomersModel.OrderEast" />
          <EntitySet Name="OrdersWest" 
                     EntityType="RegionalCustomersModel.OrderWest" />

          <AssociationSet Name="FK_OrderEast_Customer"
                 Association="RegionalCustomersModel.FK_OrderEast_Customer">
            <End Role="Customer" EntitySet="CustomersEast" />
            <End Role="OrderEast" EntitySet="OrdersEast" />
          </AssociationSet>

          <AssociationSet Name="FK_OrderWest_Customer"
                 Association="RegionalCustomersModel.FK_OrderWest_Customer">
            <End Role="Customer" EntitySet="CustomersWest" />
            <End Role="OrderWest" EntitySet="OrdersWest" />
          </AssociationSet>

        </EntityContainer>

        <EntityType Name="Customer">
          <Key>
            <PropertyRef Name="CustomerId" />
          </Key>
          <Property Name="CustomerId" Type="Int32" Nullable="false" />
          <Property Name="Name" Type="String" Nullable="false" />
          <Property Name="TotalPurchases" Type="Decimal" Nullable="false" />

          <NavigationProperty Name="OrdersEast"
          Relationship="RegionalCustomersModel.FK_OrderEast_Customer"
          FromRole="Customer" ToRole="OrderEast" />

          <NavigationProperty Name="OrdersWest"
          Relationship="RegionalCustomersModel.FK_OrderWest_Customer"
          FromRole="Customer" ToRole="OrderWest" />
        </EntityType>

        <EntityType Name="OrderEast">
          <Key>
            <PropertyRef Name="OrderId" />
          </Key>
          <Property Name="OrderId" Type="Int32" Nullable="false" />
          <Property Name="OrderTotal"
                    Type="Decimal" Nullable="false" />
          <Property Name="Tax" Type="Decimal" />
          <NavigationProperty Name="Customer"
            Relationship="RegionalCustomersModel.FK_OrderEast_Customer"
              FromRole="OrderEast" ToRole="Customer" />
        </EntityType>

        <EntityType Name="OrderWest">
          <Key>
            <PropertyRef Name="OrderId" />
          </Key>
          <Property Name="OrderId" Type="Int32" Nullable="false" />
          <Property Name="OrderTotal" Type="Decimal" Nullable="false" />
          <Property Name="Tax" Type="Decimal" />
          <NavigationProperty Name="Customer"
            Relationship="RegionalCustomersModel.FK_OrderWest_Customer"
               FromRole="OrderWest" ToRole="Customer" />
        </EntityType>

        <Association Name="FK_OrderEast_Customer">
          <End Role="Customer"
               Type="RegionalCustomersModel.Customer" Multiplicity="1" />
          <End Role="OrderEast"
               Type="RegionalCustomersModel.OrderEast" Multiplicity="*" />
        </Association>

        <Association Name="FK_OrderWest_Customer">
          <End Role="Customer"
               Type="RegionalCustomersModel.Customer" Multiplicity="1" />
          <End Role="OrderWest"
               Type="RegionalCustomersModel.OrderWest" Multiplicity="*" />
        </Association>

      </Schema>

若要實作 MEST 資料模型的儲存結構描述

  1. 實作對應至 CustomerEastCustomerWest 之個別資料表的個別實體類型。

  2. 請注意,與單一 Customer 實體類型概念結構描述不同的是,儲存結構描述中含有兩個客戶資料表。

<?xml version="1.0" encoding="utf-8"?>
<Schema Namespace="RegionalCustomersModel.Store"
              Alias="Self"
              Provider="System.Data.SqlClient"
              ProviderManifestToken="2005"
             xmlns="https://schemas.microsoft.com/ado/2006/04/edm/ssdl">

        <EntityContainer Name="dbo">

          <EntitySet Name="CustomerEast"
             EntityType="RegionalCustomersModel.Store.CustomerEast" />
          <EntitySet Name="CustomerWest"
             EntityType="RegionalCustomersModel.Store.CustomerWest" />
          <EntitySet Name="OrderEast"
             EntityType="RegionalCustomersModel.Store.OrderEast" />
          <EntitySet Name="OrderWest"
             EntityType="RegionalCustomersModel.Store.OrderWest" />

          <AssociationSet Name="FK_OrderEast_CustomerEast"
           Association="RegionalCustomersModel.Store.FK_OrderEast_CustomerEast">
            <End Role="CustomerEast" EntitySet="CustomerEast" />
            <End Role="OrderEast" EntitySet="OrderEast" />
          </AssociationSet>

          <AssociationSet Name="FK_OrderWest_CustomerWest"
           Association="RegionalCustomersModel.Store.FK_OrderWest_CustomerWest">
            <End Role="CustomerWest" EntitySet="CustomerWest" />
            <End Role="OrderWest" EntitySet="OrderWest" />
          </AssociationSet>

        </EntityContainer>

        <EntityType Name="CustomerEast">
          <Key>
            <PropertyRef Name="CustomerId" />
          </Key>
          <Property Name="CustomerId" Type="int" Nullable="false" />
          <Property Name="Name" Type="nvarchar"
                    Nullable="false" MaxLength="50" />
          <Property Name="TotalPurchases" Type="money" Nullable="false" />
        </EntityType>

        <EntityType Name="CustomerWest">
          <Key>
            <PropertyRef Name="CustomerId" />
          </Key>
          <Property Name="CustomerId" Type="int" Nullable="false" />
          <Property Name="Name" Type="nvarchar"
                    Nullable="false" MaxLength="50" />
          <Property Name="TotalPurchases" Type="money" Nullable="false" />
        </EntityType>

        <EntityType Name="OrderEast">
          <Key>
            <PropertyRef Name="OrderId" />
          </Key>
          <Property Name="OrderId" Type="int" Nullable="false" />
          <Property Name="CustomerId" Type="int" Nullable="false" />
          <Property Name="OrderTotal" Type="money" Nullable="false" />
          <Property Name="Tax" Type="money" />
        </EntityType>

        <EntityType Name="OrderWest">
          <Key>
            <PropertyRef Name="OrderId" />
          </Key>
          <Property Name="OrderId" Type="int" Nullable="false" />
          <Property Name="CustomerId" Type="int" Nullable="false" />
          <Property Name="OrderTotal" Type="money" Nullable="false" />
          <Property Name="Tax" Type="money" />
        </EntityType>

        <Association Name="FK_OrderEast_CustomerEast">
          <End Role="CustomerEast"
           Type="RegionalCustomersModel.Store.CustomerEast" Multiplicity="1" />
          <End Role="OrderEast" Type="RegionalCustomersModel.Store.OrderEast"
               Multiplicity="*" />
          <ReferentialConstraint>
            <Principal Role="CustomerEast">
              <PropertyRef Name="CustomerId" />
            </Principal>
            <Dependent Role="OrderEast">
              <PropertyRef Name="CustomerId" />
            </Dependent>
          </ReferentialConstraint>
        </Association>

        <Association Name="FK_OrderWest_CustomerWest">
          <End Role="CustomerWest"
            Type="RegionalCustomersModel.Store.CustomerWest"
               Multiplicity="1" />
          <End Role="OrderWest"
               Type="RegionalCustomersModel.Store.OrderWest"
               Multiplicity="*" />
          <ReferentialConstraint>
            <Principal Role="CustomerWest">
              <PropertyRef Name="CustomerId" />
            </Principal>
            <Dependent Role="OrderWest">
              <PropertyRef Name="CustomerId" />
            </Dependent>
          </ReferentialConstraint>
        </Association>

      </Schema>

若要使用 SQL Server Management Studio 來產生資料庫

  1. 搭配 SQL Server Management Studio 使用下列指令碼,以產生這個範例以及 HOW TO:使用每個類型的資料表繼承來定義模型 (Entity Framework) 範例中所用的資料庫。

  2. 指向 [檔案] 功能表上的 [新增],然後按一下 [Database Engine 查詢],以便使用 SQL Server Management Studio 來建立 SchoolData 資料庫和結構描述。

  3. 在 [連接到 Database Engine] 對話方塊中,輸入 localhost 或另一個 SQL Server 執行個體的名稱,然後按一下 [連接]。

  4. 將下列 Transact-SQL 指令碼貼入查詢視窗中,然後按一下 [執行]。

USE [master]
GO
CREATE DATABASE [RegionalCustomersMEST] 
GO
USE [RegionalCustomersMEST]
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].[CustomerWest]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[CustomerWest](
     [CustomerId] [int] NOT NULL,
     [Name] [nvarchar](50) NOT NULL,
     [TotalPurchases] [money] NOT NULL,
 CONSTRAINT [PK_CustomerWest] 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
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].[CustomerEast]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[CustomerEast](
     [CustomerId] [int] NOT NULL,
     [Name] [nvarchar](50) NOT NULL,
     [TotalPurchases] [money] NOT NULL,
 CONSTRAINT [PK_CustomerEast] 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
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].[OrderWest]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[OrderWest](
     [OrderId] [int] NOT NULL,
     [CustomerId] [int] NOT NULL,
     [OrderTotal] [money] NOT NULL,
     [Tax] [money] NULL,
 CONSTRAINT [PK_OrderWest] PRIMARY KEY CLUSTERED 
(
     [OrderId] 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
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].[OrderEast]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[OrderEast](
     [OrderId] [int] NOT NULL,
     [CustomerId] [int] NOT NULL,
     [OrderTotal] [money] NOT NULL,
     [Tax] [money] NULL,
 CONSTRAINT [PK_OrderEast] PRIMARY KEY CLUSTERED 
(
    [OrderId] 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
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_OrderWest_CustomerWest]') AND parent_object_id = OBJECT_ID(N'[dbo].[OrderWest]'))
ALTER TABLE [dbo].[OrderWest]  WITH CHECK ADD  CONSTRAINT [FK_OrderWest_CustomerWest] FOREIGN KEY([CustomerId])
REFERENCES [dbo].[CustomerWest] ([CustomerId])
GO
ALTER TABLE [dbo].[OrderWest] CHECK CONSTRAINT [FK_OrderWest_CustomerWest]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_OrderEast_CustomerEast]') AND parent_object_id = OBJECT_ID(N'[dbo].[OrderEast]'))
ALTER TABLE [dbo].[OrderEast]  WITH CHECK ADD  CONSTRAINT [FK_OrderEast_CustomerEast] FOREIGN KEY([CustomerId])
REFERENCES [dbo].[CustomerEast] ([CustomerId])
GO
ALTER TABLE [dbo].[OrderEast] CHECK CONSTRAINT [FK_OrderEast_CustomerEast]

若要實作 MEST 模型的對應規格

  1. 將單一 Customer 實體對應至 CustomerEastCustomerWest 的個別資料表,如下所示。

  2. 請注意實體容器對應中 CustomersEastCustomersWest 的個別實體集。

<?xml version="1.0" encoding="utf-8"?>
<Mapping Space="C-S"
               xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">

        <EntityContainerMapping
          StorageEntityContainer="dbo"
          CdmEntityContainer="RegionalCustomersEntities">

          <EntitySetMapping Name="CustomersEast">
            <EntityTypeMapping TypeName="RegionalCustomersModel.Customer">
              <MappingFragment StoreEntitySet="CustomerEast">
                <ScalarProperty Name="CustomerId" ColumnName="CustomerId" />
                <ScalarProperty Name="Name" ColumnName="Name" />
                <ScalarProperty Name="TotalPurchases"
                                ColumnName="TotalPurchases" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>

          <EntitySetMapping Name="CustomersWest">
            <EntityTypeMapping TypeName="RegionalCustomersModel.Customer">
              <MappingFragment StoreEntitySet="CustomerWest">
                <ScalarProperty Name="CustomerId" ColumnName="CustomerId" />
                <ScalarProperty Name="Name" ColumnName="Name" />
                <ScalarProperty Name="TotalPurchases"
                                ColumnName="TotalPurchases" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>

          <EntitySetMapping Name="OrdersEast" StoreEntitySet="OrderEast"
                            TypeName="RegionalCustomersModel.OrderEast">
            <ScalarProperty Name="OrderId" ColumnName="OrderId" />
            <ScalarProperty Name="OrderTotal" ColumnName="OrderTotal" />
            <ScalarProperty Name="Tax" ColumnName="Tax" />
          </EntitySetMapping>

          <EntitySetMapping Name="OrdersWest" StoreEntitySet="OrderWest"
                            TypeName="RegionalCustomersModel.OrderWest">
            <ScalarProperty Name="OrderId" ColumnName="OrderId" />
            <ScalarProperty Name="OrderTotal" ColumnName="OrderTotal" />
            <ScalarProperty Name="Tax" ColumnName="Tax" />
          </EntitySetMapping>

          <AssociationSetMapping Name="FK_OrderEast_Customer"
                 TypeName="RegionalCustomersModel.FK_OrderEast_Customer"
                 StoreEntitySet="OrderEast">
            <EndProperty Name="Customer">
              <ScalarProperty Name="CustomerId" ColumnName="CustomerId" />
            </EndProperty>
            <EndProperty Name="OrderEast">
              <ScalarProperty Name="OrderId" ColumnName="OrderId" />
            </EndProperty>
            <Condition ColumnName="CustomerId" IsNull="false" />
          </AssociationSetMapping>

          <AssociationSetMapping Name="FK_OrderWest_Customer"
                 TypeName="RegionalCustomersModel.FK_OrderWest_Customer"
                 StoreEntitySet="OrderWest">
            <EndProperty Name="Customer">
              <ScalarProperty Name="CustomerId" ColumnName="CustomerId" />
            </EndProperty>
            <EndProperty Name="OrderWest">
              <ScalarProperty Name="OrderId" ColumnName="OrderId" />
            </EndProperty>
            <Condition ColumnName="CustomerId" IsNull="false" />
          </AssociationSetMapping>

        </EntityContainerMapping>

      </Mapping>

另請參閱

工作

HOW TO:使用每個類型的多重實體來建立和執行物件查詢 (Entity Framework)
HOW TO:將物件加入特定的實體集 (Entity Framework)