다음을 통해 공유


항목 컬렉션(메타데이터)

EDM(엔터티 데이터 모델)에서 항목 컬렉션은 XML 파일이나 CLR(공용 언어 런타임) 어셈블리와 같은 영구 리소스에서 메타데이터를 로드하며, MetadataWorkspace 클래스 인스턴스에 있습니다.

ADO.NET은 메모리 내 메타데이터를 로드 및 유지하는 핵심 API인 ItemCollection 클래스를 제공합니다. ItemCollection에는 ObjectItemCollection, EdmItemCollection, StoreItemCollectionStorageMappingItemCollection과 같은 여러 파생 클래스가 있습니다. 이러한 컬렉션 클래스는 각각 다른 형식의 메타데이터용으로 디자인되었습니다. 다음 단원에서는 이러한 컬렉션 클래스가 다양한 형식의 메타데이터와 상호 작용하는 방법에 대해 설명합니다.

ObjectItemCollection

ObjectItemCollection 클래스는 개체 모델에 대한 메타데이터를 로드합니다. 개체 모델은 개념적 모델을 프로그래밍 방식으로 실현하는 데 사용할 수 있는 CLR 클래스를 나타냅니다.

ObjectItemCollectionEdmSchemaAttribute로 데코레이팅된 참조 어셈블리를 검색한 다음 어셈블리에서 ADO.NET 엔터티 프레임워크의 영구적 클래스에 해당하는 클래스를 로드합니다. 구체적으로 말하면 System.Data.Objects.DataClasses에서 파생된 클래스를 로드합니다.

참조 어셈블리에 이러한 클래스가 없는 경우 ADO.NET 메타데이터 인프라는 암시적으로 엔터티 프레임워크의 CLR 메타데이터를 로드합니다. 예를 들어 쿼리를 생성하기 위해 ObjectQuery<T> 클래스의 새 인스턴스를 만드는 경우 ADO.NET 메타데이터 인프라는 형식 매개 변수 **<T>**가 메타데이터에 이미 있는지 여부를 확인합니다. 형식 매개 변수 **<T>**가 메타데이터에 없으면 ADO.NET 메타데이터 인프라는 암시적으로 형식 매개 변수 **<T>**가 포함되어 있는 어셈블리에서 메타데이터를 로드합니다.

현재 응용 프로그램에서 명시적으로 참조하지 않는 특정 어셈블리에서 메타데이터를 로드할 수도 있습니다. 이 경우에는 MetadataWorkspace 클래스에 정의된 LoadFromAssembly 메서드 또는 ObjectItemCollection 클래스에 정의된 LoadFromAssembly 메서드를 호출할 수 있습니다.

LoadFromAssembly 메서드는 등록된 ObjectItemCollection에 내부적으로 액세스한 다음 ObjectItemCollection 클래스에 정의된 LoadFromAssembly 메서드를 호출합니다.

다음 코드 샘플에서는 특정 어셈블리에서 메타데이터를 로드하는 방법을 보여 줍니다. 이 코드 샘플에서는 AdventureWorks 모델을 사용하여 기본 데이터베이스에 대한 연결을 엽니다. 그런 다음 Human Resources Skills 모델을 명시적으로 로드합니다. AdventureWorks 및 Human Resources Skills 모델에 대한 자세한 내용은 AdventureWorks 전체 모델(EDM)Human Resources Skills WinApp(EDM 샘플 응용 프로그램)를 참조하십시오.

Imports System
Imports System.Data
Imports System.Reflection
Imports System.Collections.ObjectModel
Imports System.Data.Metadata.Edm
Imports AdventureWorksModel

Class LoadAssemblyExample

  ' This sample illustrates loading metadata from a specific assembly.
  Public Shared Sub Main()
    Try
      ' Retrieve a MetadataWorkspace from an ObjectContext:
      ' AdventureWorksEntities represents the ADO.NET ObjectContext 
      ' that acts as a factory for queries; 
      ' tracks object state; and is used to initiate 
      ' updates against the database.
      Using db As AdventureWorksEntities = New AdventureWorksEntities

         ' Dereference the workspace from the AdventureWorksEntities 
         ' class 
         ' (an ObjectContext specialization).
         Dim workspace As MetadataWorkspace = db.MetadataWorkspace

         ' Load metadata from the HRSkills assembly.
         workspace.LoadFromAssembly(Assembly.Load("HRSkills"))

         ' Get a collection of the EdmTypes from the object model.
         Dim types As ReadOnlyCollection(Of EdmType) = _
            workspace.GetItems(Of EdmType)(DataSpace.OSpace)

         ' Iterate through the collection to get each EdmType.
         Dim item As EdmType
         For Each item In types
             Console.WriteLine("Type: {0}, Type in Model: {1} ", _
                     item.GetType.FullName, item.FullName)
         Next
      End Using
    Catch exceptionMetadata As MetadataException
       Console.WriteLine("MetadataException: {0}", _
          exceptionMetadata.Message)
    End Try
  End Sub
End Class
using System;
using System.Data;
using System.Reflection;
using System.Collections.ObjectModel;
using System.Data.Metadata.Edm;
using AdventureWorksModel;

class LoadAssemblyExample
{
  // This sample illustrates loading metadata from a specific assembly.
  static void Main()
  {
     try
     {
        // Retrieve a MetadataWorkspace from an ObjectContext:
        // AdventureWorksEntities represents the ADO.NET ObjectContext 
        // that acts as a factory for queries; 
        // tracks object state; and is used to initiate 
        // updates against the database.
       using (
           AdventureWorksEntities db = new AdventureWorksEntities ())
        {
           // Dereference the workspace from the AdventureWorksEntities 
           // class (an ObjectContext specialization).
           MetadataWorkspace workspace = db.MetadataWorkspace;

           // Load metadata from the HRSkills assembly.
           workspace.LoadFromAssembly(
                   Assembly.Load(@"HRSkills"));

           // Get a collection of the EdmTypes from the object model.
           ReadOnlyCollection<EdmType> types =
               workspace.GetItems<EdmType>(DataSpace.OSpace);

           // Iterate through the collection to get each EdmType.
           foreach (EdmType item in types)
           {
               Console.WriteLine("Type: {0}, Type in Model: {1} ",
                      item.GetType().FullName, item.FullName);
           }
       }
     }
     catch (MetadataException exceptionMetadata)
     {
        Console.WriteLine("MetadataException: {0}",
                         exceptionMetadata.Message);
     }
  }
}

EdmItemCollection

EdmItemCollection 클래스는 개념적 모델에 대한 메타데이터를 로드합니다. EdmItemCollection 클래스는 개념적 모델의 XML 표현인 CSDL(개념 스키마 정의 언어) 파일에서 메타데이터를 로드합니다.

파일과 같은 영구 소스, 어셈블리의 리소스 또는 XmlReader에서 EdmItemCollectionStoreItemCollection을 만들 수 있습니다.

StoreItemCollection

StoreItemCollection 클래스는 저장소(데이터베이스) 모델에 대한 메타데이터를 로드합니다. StoreItemCollection 클래스는 저장소 모델의 XML 표현인 SSDL(저장소 스키마 정의 언어) 파일에서 메타데이터를 로드합니다.

파일과 같은 영구 소스, 어셈블리의 리소스 또는 XmlReader에서 EdmItemCollectionStoreItemCollection을 만들 수 있습니다.

StorageMappingItemCollection

StorageMappingItemCollection 클래스는 개념적 모델과 저장소(데이터베이스) 모델 사이의 매핑을 나타내는 메타데이터를 로드합니다. StorageMappingItemCollection 클래스는 개념적 모델과 저장소 모델 사이의 매핑에 대한 XML 표현인 MSL(매핑 사양 언어) 파일에서 메타데이터를 로드합니다.

참고 항목

개념

메타데이터 작업 영역