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

In questo argomento viene descritto come creare un modello concettuale con più set di entità per tipo (MEST, Multiple Entity Sets per Type). La definizione di più set di entità per tipo consente di semplificare il codice quando più tabelle nel database sottostante hanno la stessa struttura. Se si utilizzano tipi di entità che non presentano associazioni ad altri tipi, la definizione di un modello MEST è piuttosto semplice. Per definire invece un modello MEST per tipi di entità che non presentano associazioni ad altri tipi, è necessario implementare MEST per ogni tipo nell'oggetto grafico. Per ulteriori informazioni, vedere MEST - Cos'è e come funziona In questo argomento viene descritto come definire un modello MEST per un tipo di entità che non presenta associazioni con gli altri tipi.

È consigliabile implementare MEST solo quando le tabelle di database sottostanti hanno la stessa struttura.

I passaggi di base per la definizione di un modello MEST sono i seguenti:

  1. Utilizzare più elementi EntitySet, ognuno con lo stesso valore per l'attributo EntityType, per definire più set di entità per un determinato tipo nel modello concettuale.

  2. Eseguire il mapping di ogni set di entità alla tabella appropriata in MSL (Mapping Specification Language). Per ulteriori informazioni, vedere Elemento EntitySetMapping (MSL).

Nell'esempio riportato di seguito si suppone che sia stato installato il database di esempio seguente:

USE [master]
GO
CREATE DATABASE [TestDB] 
GO

SET QUOTED_IDENTIFIER OFF;
SET ANSI_NULLS ON;
GO

USE [TestDB]
GO

-- --------------------------------------------------
-- Create Tables
-- --------------------------------------------------

-- Creating table 'GraduateCourses'
CREATE TABLE [dbo].[GraduateCourses] (
    [GraduateCourseId] int  NOT NULL,
    [Title] nvarchar(max)  NOT NULL,
    [Credits] int  NOT NULL
);
GO
-- Creating table 'UnderGraduateCourses'
CREATE TABLE [dbo].[UnderGraduateCourses] (
    [UnderGraduateCourseId] int  NOT NULL,
    [Title] nvarchar(max)  NOT NULL,
    [Credits] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Primary Key Constraints
-- --------------------------------------------------

-- Creating primary key in table 'GraduateCourses'
ALTER TABLE [dbo].[GraduateCourses] WITH NOCHECK 
ADD CONSTRAINT [PK_GraduateCourses]
    PRIMARY KEY CLUSTERED ([GraduateCourseId] ASC)
    ON [PRIMARY]
GO
-- Creating primary key in table 'UnderGraduateCourses'
ALTER TABLE [dbo].[UnderGraduateCourses] WITH NOCHECK 
ADD CONSTRAINT [PK_UnderGraduateCourses]
    PRIMARY KEY CLUSTERED ([UnderGraduateCourseId] ASC)
    ON [PRIMARY]
GO

Nell'esempio si suppone inoltre che il progetto sia stato configurato per l'utilizzo di . Per ulteriori informazioni, vedere Configurazione di Entity Framework (attività di Entity Framework).

Per creare il modello di archiviazione

  1. Aggiungere il file XML seguente al progetto e denominarlo MEST.ssdl.

    -oppure-

    Aggiungere un file edmx vuoto (MEST.edmx) al progetto e sostituire l'elemento Schema nell'elemento edmx:StorageModels con l'elemento Schema nel file XML che segue. Per ulteriori informazioni, vedere How to: Create a New .edmx File e .edmx File Overview.

    Notare che le tabelle nel modello di archiviazione presentano la stessa struttura.

    <?xml version="1.0" encoding="utf-8" ?>
    <Schema Namespace="MEST.Store" Alias="Self" Provider="System.Data.SqlClient"
                ProviderManifestToken="2008"
                xmlns:store="https://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator"
                xmlns="https://schemas.microsoft.com/ado/2009/02/edm/ssdl">
      <EntityContainer Name="MESTStoreContainer">
        <EntitySet Name="GraduateCourses"
                   EntityType="MEST.Store.GraduateCourses"
                   store:Type="Tables" Schema="dbo" />
        <EntitySet Name="UnderGraduateCourses"
                   EntityType="MEST.Store.UnderGraduateCourses"
                   store:Type="Tables" Schema="dbo" />
      </EntityContainer>
      <EntityType Name="GraduateCourses">
        <Key>
          <PropertyRef Name="GraduateCourseId" />
        </Key>
        <Property Name="GraduateCourseId" Type="int" Nullable="false" />
        <Property Name="Title" Type="nvarchar(max)" Nullable="false" />
        <Property Name="Credits" Type="int" Nullable="false" />
      </EntityType>
      <EntityType Name="UnderGraduateCourses">
        <Key>
          <PropertyRef Name="UnderGraduateCourseId" />
        </Key>
        <Property Name="UnderGraduateCourseId" Type="int" Nullable="false" />
        <Property Name="Title" Type="nvarchar(max)" Nullable="false" />
        <Property Name="Credits" Type="int" Nullable="false" />
      </EntityType>
    </Schema>
    

Per creare il modello concettuale

  1. Aggiungere il file XML seguente al progetto e denominarlo MEST.csdl.

    -oppure-

    Nel file edmx sostituire l'elemento Schema nell'elemento edmx:ConceptualModels con l'elemento Schema nel file XML che segue.

    Notare che per il tipo di entità Course sono stati definiti due set di entità.

    <?xml version="1.0" encoding="utf-8" ?>
    <Schema xmlns="https://schemas.microsoft.com/ado/2008/09/edm"
                  xmlns:cg="https://schemas.microsoft.com/ado/2006/04/codegeneration"
                  xmlns:store="https://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator"
                  Namespace="MEST" Alias="Self"
                  xmlns:annotation="https://schemas.microsoft.com/ado/2009/02/edm/annotation">
      <EntityContainer Name="MESTContainer" annotation:LazyLoadingEnabled="true">
        <EntitySet Name="GraduateCourses" EntityType="MEST.Course" />
        <EntitySet Name="UnderGraduateCourses" EntityType="MEST.Course" />
      </EntityContainer>
      <EntityType Name="Course">
        <Key>
          <PropertyRef Name="CourseId" />
        </Key>
        <Property Type="Int32" Name="CourseId" Nullable="false" />
        <Property Type="String" Name="Title" Nullable="false" />
        <Property Type="Int32" Name="Credits" Nullable="false" />
      </EntityType>
    </Schema>
    

Per definire il mapping tra il modello concettuale e il modello di archiviazione

  1. Aggiungere il file XML seguente al progetto e denominarlo MEST.msl.

    -oppure-

    Nel file edmx sostituire l'elemento Mapping nell'elemento edmx:Mappings con l'elemento Mapping nel file XML che segue.

    Notare che ogni set di entità è mappato al database sottostante appropriato.

    <?xml version="1.0" encoding="utf-8" ?>
    <Mapping Space="C-S"
                 xmlns="https://schemas.microsoft.com/ado/2008/09/mapping/cs">
      <EntityContainerMapping StorageEntityContainer="MESTStoreContainer"
                              CdmEntityContainer="MESTContainer">
        <EntitySetMapping Name="GraduateCourses">
          <EntityTypeMapping TypeName="IsTypeOf(MEST.Course)">
            <MappingFragment StoreEntitySet="GraduateCourses">
              <ScalarProperty Name="CourseId" ColumnName="GraduateCourseId" />
              <ScalarProperty Name="Title" ColumnName="Title" />
              <ScalarProperty Name="Credits" ColumnName="Credits" />
            </MappingFragment>
          </EntityTypeMapping>
        </EntitySetMapping>
        <EntitySetMapping Name="UnderGraduateCourses">
          <EntityTypeMapping TypeName="IsTypeOf(MEST.Course)">
            <MappingFragment StoreEntitySet="UnderGraduateCourses">
              <ScalarProperty Name="CourseId" ColumnName="UnderGraduateCourseId" />
              <ScalarProperty Name="Title" ColumnName="Title" />
              <ScalarProperty Name="Credits" ColumnName="Credits" />
            </MappingFragment>
          </EntityTypeMapping>
        </EntitySetMapping>
      </EntityContainerMapping>
    </Mapping>
    

Vedere anche

Altre risorse

Specifiche CSDL, SSDL e MSL
Definizione di modelli di dati avanzati (attività di Entity Framework)
ADO.NET Entity Data Model Tools