Share via


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

本主題將描述如何使用每個類型的多重實體 (MEST) 來建立概念模型。 當基礎資料庫中的多個資料表都有相同的結構時,定義每個類型的多重實體可讓您簡化程式碼。 當您要使用與其他類型沒有關聯的實體類型時,定義 MEST 模型是最直接的方式。 不過,若要針對與其他類型沒有關聯的實體類型定義 MEST 模型,您必須針對物件圖形中的每個類型實作 MEST。 如需詳細資訊,請參閱 MEST - 這是什麼?如何運作? (英文)。 本主題將描述如何針對與其他類型沒有關聯的實體類型定義 MEST 模型。

請注意,只有當基礎資料庫資料表具有相同的結構時,您才應該實作 MEST。

定義 MEST 模型的基本步驟如下所示:

  1. 使用多個 EntitySet 項目 (每個項目的 EntityType 屬性值都相同),針對概念模型中的給定類型定義多個實體集。

  2. 使用對應規格語言 (MSL),將每個實體集對應至適當的資料表。 如需詳細資訊,請參閱 EntitySetMapping 項目 (MSL)

下面的範例會假設您已經安裝了下列範例資料庫:

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

此範例也會假設您已經將專案設定為使用 。 如需詳細資訊,請參閱設定 Entity Framework (Entity Framework 工作)

若要建立儲存體模型

  1. 將下列 XML 檔案加入至專案,並將它命名為 MEST.ssdl

    -或-

    將空的 .edmx 檔案 (MEST.edmx) 加入至專案,並且將 edmx:StorageModels 項目底下的 Schema 項目取代成下列 XML 檔案中的 Schema 項目。 如需詳細資訊,請參閱 How to: Create a New .edmx File.edmx File Overview

    請注意,儲存體模型中的資料表具有相同的結構。

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

若要建立概念模型

  1. 將下列 XML 檔案加入至專案,並將它命名為 MEST.csdl

    -或-

    在您的 .edmx 檔案中,將 edmx:ConceptualModels 項目中的 Schema 項目取代成下列 XML 檔案中的 Schema 項目。

    請注意,目前已經針對 Course 實體類型定義了兩個實體集。

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

若要定義概念模型與儲存體模型之間的對應

  1. 將下列 XML 檔案加入至專案,並將它命名為 MEST.msl

    -或-

    在您的 .edmx 檔案中,將 edmx:Mappings 項目中的 Mapping 項目取代成下列 XML 檔案中的 Mapping 項目。

    請注意,每個實體集都會對應至適當的基礎資料庫。

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

另請參閱

其他資源

CSDL、SSDL 和 MSL 規格
定義進階資料模型 (Entity Framework 工作)
ADO.NET Entity Data Model Tools