방법: 형식별 다중 엔터티 집합으로 모델 정의(Entity Framework)

이 항목에서는 MEST(형식별 다중 엔터티 집합)를 사용하여 개념적 모델을 만드는 방법에 대해 설명합니다. 형식별 다중 엔터티 집합을 정의하면 기본 데이터베이스에 있는 여러 테이블의 구조가 동일한 경우에 코드를 효율적으로 사용할 수 있습니다. 다른 형식과 연결되지 않은 엔터티 형식으로 작업하는 경우에는 MEST 모델을 간단하게 정의할 수 있습니다. 그러나 다른 형식과 연결된 엔터티 형식에 대한 MEST 모델을 정의하려면 개체 그래프의 각 형식에 대해 MEST를 구현해야 합니다. 자세한 내용은 MEST - What is it and how does it work?를 참조하십시오. 이 항목에서는 다른 형식과 연결되지 않은 엔터티 형식에 대한 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