Udostępnij za pośrednictwem


How to: Define a Model with Multiple Entity Sets per Type

The topic describes how to create a conceptual model with multiple entity sets per type (MEST). Defining multiple entity sets per type allows you to streamline your code when multiple tables in the underlying database have the same structure. When you are working with entity types that do not have associations to other types, defining a MEST model is straightforward. However, to define a MEST model for entity types that do have associations with other types, you must implement MEST for each type in the object graph. For more information, see MEST - What is it and how does it work? This topic describes how to define a MEST model for an entity type that does not have associations with other types.

Note that you should only implement MEST when the underlying database tables have the same structure.

The basic steps for defining a MEST model are as follows:

  1. Use multiple EntitySet elements (each with the same value for the EntityType attribute) to define multiple entity sets for a given type in the conceptual model.

  2. Map each entity set to the appropriate table in the mapping specification language (MSL). For more information, see EntitySetMapping Element (MSL).

The example below assumes that you have installed the following sample database:

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

The example also assumes that you have configured your project to use the . For more information, see Configuring the Entity Framework.

To create the storage model

  1. Add the following XML file to your project and name it MEST.ssdl.

    -OR-

    Add an empty .edmx file (MEST.edmx) to your project and replace the Schema element under the edmx:StorageModels element with the Schema element in the following XML file. For more information, see How to: Create a New .edmx File and .edmx File Overview.

    Note that the tables in the storage model have the same structure.

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

To create the conceptual model

  1. Add the following XML file to your project and name it MEST.csdl.

    -OR-

    In your .edmx file, replace the Schema element in the edmx:ConceptualModels element with the Schema element in the following XML file.

    Note that two entity sets have been defined for the Course entity type.

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

To define the mapping between the conceptual model and the storage model

  1. Add the following XML file to your project and name it MEST.msl.

    -OR-

    In your .edmx file, replace the Mapping element in the edmx:Mappings element with the Mapping element in the following XML file.

    Note that each entity set is mapped to the appropriate underlying database.

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

See Also

Other Resources

CSDL, SSDL, and MSL Specifications
Defining Advanced Data Models
ADO.NET Entity Data Model Tools