Share via


구조 형식(메타데이터)

EDM(엔터티 데이터 모델)에서 구조 형식은 멤버가 있는 EDM 형식입니다. 멤버는 StructuralType 클래스에서 파생된 형식의 내용을 정의합니다. StructuralType 클래스에는 EntityType, RelationshipTypeComplexType과 같은 몇 가지 파생 형식이 있습니다.

EntityType은 EDM에서 고객이나 주문과 같은 최상위 개념을 나타냅니다. RelationshipType은 EDM에서 연결을 나타내는 AssociationType의 기본 형식입니다. EDM의 엔터티 및 연결에 대한 자세한 내용은 엔터티 형식(EDM)Association(EDM)을 참조하십시오.

ComplexType은 엔터티 형식과 같은 속성 집합은 포함되지만 키 속성은 포함되지 않는 형식을 나타냅니다. EDM의 복합 형식에 대한 자세한 내용은 복합 형식(EDM)을 참조하십시오.

EDM 스키마의 EntityType, RelationshipTypeComplexType에는 하위 요소 또는 멤버가 있습니다. 예를 들어 엔터티 형식의 속성은 해당 멤버입니다. 마찬가지로 관계의 끝도 해당 멤버입니다. 각 멤버에는 선언 형식이 있으며 Null 허용 제약 조건 또는 기본값 할당이 있을 수 있습니다.

다음 단원에서는 멤버에 대한 자세한 정보를 제공합니다.

멤버

EDM에서 멤버는 StructuralType 클래스에서 파생된 형식의 내용을 정의합니다. 예를 들어 다음 XML 표현은 AdventureWorks 전체 개념 스키마(EDM) 항목에 있는 AdventureWorks 개념 스키마 파일(.csdl)의 일부입니다. 이 XML 표현은 Department EntityType을 정의합니다. Department 엔터티의 멤버는 DepartmentID, Name, GroupName, ModifiedDateEmployeeDepartmentHistory입니다.

<EntityType Name="Department">
    <Key>
      <PropertyRef Name="DepartmentID" />
    </Key>
    <Property Name="DepartmentID" Type="Int16" Nullable="false" />
    <Property Name="Name" Type="String" Nullable="false" />
    <Property Name="GroupName" Type="String" Nullable="false" />
    <Property Name="ModifiedDate" Type="DateTime" Nullable="false" />
    <NavigationProperty Name="EmployeeDepartmentHistory" Relationship="Adventureworks.FK_EmployeeDepartmentHistory_Department_DepartmentID" FromRole="Department" ToRole="EmployeeDepartmentHistory" />
</EntityType>

StructuralType에서 제공하는 System.Data.Metadata.Edm.StructuralType.Members 속성을 사용하여 모든 구조 형식에 대한 멤버 목록을 가져올 수 있습니다. Members 속성은 EdmMember 개체를 보유하는 ReadOnlyMetadataCollection을 반환합니다.

StructuralType에서 파생되는 형식은 해당 멤버를 StructuralType에서 상속되는 Members 속성에 저장합니다.

각 파생 형식에서는 StructuralType에서 상속되는 Members 속성 외에도 해당 형식에 대해 선언할 수 있는 특수 멤버의 목록을 가져오기 위한 몇 가지 속성을 추가로 제공합니다. 예를 들어 System.Data.Metadata.Edm.EntityType.PropertiesEdmProperty 개체를 보유하는 컬렉션을 반환합니다.

마찬가지로 RelationshipType에는 RelationshipEndMember 개체의 컬렉션을 반환하는 System.Data.Metadata.Edm.RelationshipType.RelationshipEndMembers 속성이 포함됩니다. EdmPropertyRelationshipEndMember는 모두 EdmMember에서 파생됩니다.

다음은 구조 형식의 멤버를 검색하는 데 사용할 수 있는 속성 집합 목록입니다.

  • EntityType 개체의 모든 멤버 목록을 가져옵니다. EntityType의 멤버에는 속성, 탐색 속성 및 EntityType 개체의 키 멤버가 포함될 수 있습니다. System.Data.Metadata.Edm.EntityType.Members 속성은 StructuralType 클래스에서 상속됩니다. 개념 스키마에서 EntityType의 멤버를 지정하는 방법에 대한 자세한 내용은 EntityType 요소(CSDL)를 참조하십시오.

  • EntityType 개체의 속성 목록을 가져옵니다. 개념 스키마에서 EntityType의 속성을 지정하는 방법에 대한 자세한 내용은 엔터티 형식(EDM)EntityType 요소(CSDL)를 참조하십시오.

  • EntityType 개체의 탐색 속성 목록을 가져옵니다. 개념 스키마에서 EntityType의 탐색 속성을 지정하는 방법에 대한 자세한 내용은 엔터티 형식(EDM)EntityType 요소(CSDL)를 참조하십시오.

  • EntityType 개체의 키 멤버 목록을 가져옵니다. 이 속성은 EntityTypeBase 클래스에서 상속됩니다.

  • RelationshipType 개체의 End 멤버 목록을 가져옵니다.

  • AssociationType 개체의 End 멤버 목록을 가져옵니다. 개념 스키마에서 AssociationTypeEnd 멤버를 지정하는 방법에 대한 자세한 내용은 Association 요소(CSDL)를 참조하십시오.

  • ComplexType 개체의 속성 목록을 가져옵니다. 개념 스키마에서 ComplexType의 속성을 지정하는 방법에 대한 자세한 내용은 복합 형식(EDM)방법: 복합 형식으로 모델 정의(Entity Framework)를 참조하십시오. ComplexType 클래스의 코드 샘플은 지정된 모델에서 복합 형식의 속성을 검색하는 방법을 보여 줍니다.

다음 코드 샘플에서는 연결에서 메타데이터 작업 영역을 가져온 다음 이 메타데이터 작업 영역을 사용하여 지정된 모델에서 엔터티의 멤버 및 관계 형식에 대한 정보를 검색하는 방법을 보여 줍니다. 메타데이터 작업 영역은 메타데이터 검색을 지원하는 런타임 서비스 구성 요소입니다.

코드 샘플에서는 CSpace를 사용하여 모델을 지정합니다. CSpace는 개념적 모델의 기본 이름을 나타냅니다. 코드 샘플에서는 AdventureWorks 전체 모델(EDM) 항목에서 제공하는 AdventureWorks 모델을 사용합니다. 응용 프로그램 구성 파일의 예를 보려면 AdventureWorks 개체 모델 사용(EDM)을 참조하십시오.

using System;
using System.Data;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;

class GetMembersExample
{
  static void Main()
  {
    try
    {
      // Establish a connection to the underlying data provider by 
      // using the connection string specified in the config file.
      using (EntityConnection connection = 
              new EntityConnection("Name=AdventureWorksEntities"))
      {
         // Open the connection.
         connection.Open();

         // Access the metadata workspace.
         MetadataWorkspace workspace = 
           connection.GetMetadataWorkspace();

         // Get members of entity types and relationship types.
         GetMembers(workspace, DataSpace.CSpace);
      }
    }
    catch (MetadataException exceptionMetadata)
    {
       Console.WriteLine("MetadataException: {0}", 
             exceptionMetadata.Message);
    }
    catch (System.Data.MappingException exceptionMapping)
    {
        Console.WriteLine("MappingException: {0}",
                          exceptionMapping.Message);
    }
  }

  public static void GetMembers(
    MetadataWorkspace workspace, DataSpace model)
  {
    // Get a collection of entity types.
    ReadOnlyCollection<EntityType> entityTypes = 
            workspace.GetItems<EntityType>(model);

    // Iterate through the collection to get each entity type.
    foreach (EntityType entityType in entityTypes)
    {
       Console.WriteLine(
          "\n\n***EntityType Name: {0}, Namespace: {1}, RefType: {2}",
          entityType.Name,
          entityType.NamespaceName,
          entityType.GetReferenceType());
          
       Console.WriteLine(
          "\nGet all members of the current EntityType object ==>");
          // Iterate through the collection to get all members of the 
       // current EntityType object.
       foreach (EdmMember member in entityType.Members)
       {
          Console.Write("   Member Name: {0} ", member.Name);
       }

       Console.WriteLine(
           "\nGet only the properties of the current "+
           "EntityType object ==>");
       // Iterate through the collection to get each property of the 
       // current EntityType object.
       foreach (EdmProperty property in entityType.Properties)
       {
          Console.Write("   Property Name: {0} ", property.Name);
          Console.WriteLine(
                "   Property declaring Type: {0}, edmtype: {1}," + 
                " default: {2}, nullable: {3} ", 
                property.DeclaringType, property.TypeUsage.EdmType, 
               property.Default, property.Nullable);
       }

       Console.WriteLine("\nGet only the navigation properties of " +
             "the current EntityType object ==>");
       // Iterate through the collection to get each navigation 
       // property of the current EntityType object.
       foreach (NavigationProperty property in 
                      entityType.NavigationProperties)
       {
          Console.Write(
                "Name: {0}, RelationshipTypeName: {1}, " + 
                "ToEndMemberName: {2} ",
                property.Name, property.RelationshipType.Name, 
                property.ToEndMember.Name);
       }

       Console.WriteLine("\nGet only the key members of the " + 
            "current EntityType object ==>");
         // Iterate through the collection to get each key members of 
         // the current EntityType object.
         ReadOnlyMetadataCollection<EdmMember> collectionKeyMembers = 
             entityType.KeyMembers;
         if (collectionKeyMembers.Count != 0)
         {
            Console.Write("   Key: {0} ", 
                    GetKeys(collectionKeyMembers));
          }
     }

     // Get a collection of relationship types.
     ReadOnlyCollection<RelationshipType> relationshipTypes = 
        workspace.GetItems<RelationshipType>(model);

     // Iterate through the collection to get each relationship type.
     foreach (RelationshipType relationType in relationshipTypes)
     {
        Console.WriteLine(
               "\n\nRelationshipType Name: {0}, Namespace: {1}",
               relationType.Name,
               relationType.NamespaceName);
        Console.WriteLine(
             "\nGet all members of the current "+
             "RelationshipType object ==>");
           // Iterate through the collection to get all members of the
           // current RelationshipType object.
        foreach (EdmMember member in relationType.Members)
        {
            Console.Write("   Member Name: {0} ", member.Name);
        }

        Console.WriteLine(
            "\nGet only the end members of the current " +
            "RelationshipType object ==>");
         // Iterate through the collection to get only the end 
         // members of
         // the current RelationshipType object.
         foreach (RelationshipEndMember endMember in 
             relationType.RelationshipEndMembers)
         {
            Console.Write("   End Member Name: {0} ", endMember.Name);
         }
      }
  }

  // Returns the keys in a string format.
  private static string GetKeys(ICollection<EdmMember> keyMembers)
  {
    StringBuilder result = new StringBuilder();
    foreach (EdmMember member in keyMembers)
    {
        if (result.Length != 0)
        {
            result.Append(" ");
         }
        result.Append(member.Name);
    }
    return result.ToString();
  }
}
Imports System
Imports System.Data
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Text
Imports System.Data.EntityClient
Imports System.Data.Metadata.Edm

Class GetMembersExample
  Public Shared Sub Main()
    Try
      ' Establish a connection to the underlying data provider by 
      ' using the connection string specified in the config file.
      Using connection As EntityConnection = _
         New EntityConnection("Name=AdventureWorksEntities")

         ' Open the conection.
         connection.Open()

         ' Access the metadata workspace.
         Dim workspace As MetadataWorkspace = _
           connection.GetMetadataWorkspace

         ' Get members of entity types and relationship types.
         GetMembers(workspace, DataSpace.CSpace)
      End Using
    Catch exceptionMetadata As MetadataException
        Console.WriteLine("MetadataException: {0}", _
           exceptionMetadata.Message)
    Catch exceptionMapping As MappingException
        Console.WriteLine("MappingException: {0}", _
            exceptionMapping.Message)
    End Try
  End Sub

  Public Shared Sub GetMembers(ByVal workspace As MetadataWorkspace, _
      ByVal model As DataSpace)

    ' Get a collection of entity types.
    Dim entityTypes As ReadOnlyCollection(Of EntityType) = _
        workspace.GetItems(Of EntityType)(model)

    ' Iterate through the collection to get each entity type.
    Dim entityType As EntityType
    For Each entityType In entityTypes
       Console.WriteLine( _
         ControlChars.Lf & ControlChars.Lf & _
         "***EntityType Name: {0}, Namespace: {1}, RefType: {2}", _
         entityType.Name, entityType.NamespaceName, _
         entityType.GetReferenceType)

       Console.WriteLine(ControlChars.Lf & _
         "Get all members of the current EntityType object ==>")

       ' Iterate through the collection to get all members of the 
       ' current EntityType object.
       Dim member As EdmMember
       For Each member In entityType.Members
         Console.Write("   Member Name: {0} ", member.Name)
       Next

       Console.WriteLine(ControlChars.Lf & _
        "Get only the properties of the current EntityType object ==>")

       ' Iterate through the collection to get each property of the 
       ' current EntityType object.
       Dim property1 As EdmProperty
       For Each property1 In entityType.Properties
          Console.Write("   Property Name: {0} ", property1.Name)
          Console.WriteLine( _
           "   Property declaring Type: {0}, edmtype: {1}, default: {2}, nullable: {3} ", _
           New Object() {property1.DeclaringType, _
           property1.TypeUsage.EdmType, _
           property1.Default, property1.Nullable})
        Next

        Console.WriteLine(ControlChars.Lf & _
          "Get only the navigation properties of the current EntityType object ==>")

        ' Iterate through the collection to get each navigation 
        ' property of the current EntityType object.
        Dim property2 As NavigationProperty
        For Each property2 In entityType.NavigationProperties
          Console.Write( _
       "Name: {0}, RelationshipTypeName: {1}, ToEndMemberName: {2} ", _
          property2.Name, property2.RelationshipType.Name, _
          property2.ToEndMember.Name)
        Next

          Console.WriteLine(ControlChars.Lf & _
       "Get only the key members of the current EntityType object ==>")
          ' Iterate through the collection to get each key members of 
          ' the current EntityType object.
          Dim collectionKeyMembers As _
            ReadOnlyMetadataCollection(Of EdmMember) = _
            entityType.KeyMembers
          If (collectionKeyMembers.Count <> 0) Then
           Console.Write("   Key: {0} ", GetKeys(collectionKeyMembers))
          End If
        Next

        ' Get a collection of relationship types.
        Dim relationshipTypes As _
           ReadOnlyCollection(Of RelationshipType) = _
           workspace.GetItems(Of RelationshipType)(model)

        ' Iterate through the collection to get each relationship type.
        Dim relationType As RelationshipType
        For Each relationType In relationshipTypes
          Console.WriteLine(ControlChars.Lf & ControlChars.Lf & _
            "RelationshipType Name: {0}, Namespace: {1}", _
             relationType.Name, relationType.NamespaceName)
          Console.WriteLine(ControlChars.Lf & _
          "Get all members of the current RelationshipType object ==>")

          ' Iterate through the collection to get all members of the
          ' current RelationshipType object.
          Dim member As EdmMember
          For Each member In relationType.Members
              Console.Write("   Member Name: {0} ", member.Name)
          Next

          Console.WriteLine(ControlChars.Lf & _
           "Get only the end members of the current RelationshipType object ==>")
          Dim endMember As RelationshipEndMember

          ' Iterate through the collection to get only the 
          ' end members of
          ' the current RelationshipType object.
          For Each endMember In relationType.RelationshipEndMembers
            Console.Write("   End Member Name: {0} ", endMember.Name)
          Next
      Next
  End Sub

  Public Shared Function GetKeys(ByVal keyMembers As _
      ICollection(Of EdmMember)) As String
    Dim result As New StringBuilder
    Dim member As EdmMember
    For Each member In keyMembers
       If (result.Length <> 0) Then
            result.Append(" ")
       End If
       result.Append(member.Name)
    Next
      Return result.ToString
  End Function
End Class

참고 항목

개념

형식(메타데이터)
메타데이터 형식 계층 구조
메타데이터 형식 계층 구조 개요