Condividi tramite


Procedura: definire un modello con più set di entità per tipo (Entity Framework)

Il modello EDM (Entity Data Model) consente l'inclusione di un tipo di entità in più set di entità all'interno di un singolo contenitore di entità o l'inclusione di un tipo di entità in set di entità in più contenitori di entità. La definizione di più set di entità per tipo (MEST, Multiple Entity Sets per Type) consente agli utenti di semplificare il codice quando i database sono partizionati o in altri scenari in cui più tabelle hanno la stessa struttura. Per ulteriori informazioni, vedere Set di entità (EDM).

Per implementare lo schema concettuale per il modello di dati MEST

  1. Creare un progetto Libreria di classi e aggiungere un nuovo modello EDM.

  2. Implementare un tipo di entità Customer incluso in due set di entità per i clienti di una regione orientale, CustomersEast, e una regione occidentale, CustomersWest.

  3. Si noti la differenza tra il singolo tipo di entità Customer utilizzato nei due set di entità per i clienti di diverse regioni e i due tipi di ordine, OrderEast e OrderWest, nei set di entità OrdersEast e OrdersWest.

  4. Implementare le associazioni tra il singolo tipo Customer e i due tipi di ordine regionali, OrderEast e OrderWest, per riflettere la struttura MEST. Entrambe le associazioni tra il tipo Customer e i tipi di ordine regionali specificano il tipo Customer dell'entità finale dell'associazione con una cardinalità pari a uno.

  5. Implementare due set di entità che contengono il tipo Customer nello schema CSDL (Conceptual Schema Definition Language). Entrambi i set di entità CustomersEast e CustomersWest specificano RegionalCustomersModel.Customer come tipi di entità.

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

Per implementare lo schema di archiviazione per un modello di dati MEST

  1. Implementare tipi di entità separati corrispondenti alle diverse tabelle per CustomerEast e CustomerWest.

  2. Si noti che nello schema di archiviazione sono presenti due tabelle dei clienti, a differenza dello schema concettuale del singolo tipo di entità 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>

Per generare il database tramite SQL Server Management Studio

  1. Utilizzare lo script seguente con SQL Server Management Studio per generare il database utilizzato in questo esempio e nell'esempio dell'argomento Procedura: definire un modello con ereditarietà tabella per tipo (Entity Framework).

  2. Scegliere Nuovo dal menu File, quindi fare clic su Query del Motore di database per creare lo schema e il database SchoolData con SQL Server Management Studio.

  3. Nella finestra di dialogo Connetti al Motore di database digitare localhost o il nome di un'altra istanza di SQL Server, quindi fare clic su Connetti.

  4. Incollare lo script Transact-SQL seguente nella finestra di query, quindi fare clic su Esegui.

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]

Per implementare la specifica di mapping per il modello MEST

  1. Mappare la singola entità Customer a tabelle distinte per CustomerEast e CustomerWest, come illustrato di seguito.

  2. Si notino i set di entità separati per CustomersEast e CustomersWest nel mapping del contenitore di entità.

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

Vedere anche

Attività

Procedura: creare ed eseguire query di oggetto utilizzando più set di entità per tipo (Entity Framework)
Procedura: aggiungere un oggetto a un set di entità specifico (Entity Framework)