관계(메타데이터)
EDM(엔터티 데이터 모델)의 관계는 두 엔터티가 관련되는 방법을 정의합니다. EDM의 관계에 대한 자세한 내용은 엔터티 데이터 모델 관계 항목에서 설명합니다.
ADO.NET 메타데이터는 EntityType 간의 관계를 나타내는 AssociationType을 제공합니다. AssociationType은 RelationshipType에서 파생되며 EDM 연결을 나타냅니다. 마찬가지로 AssociationSet은 특정 EDM 연결에 참여하는 EntitySet을 정의합니다. 엔터티 및 연결 집합에 대한 자세한 내용은 엔터티 집합(EDM) 및 연결 집합(EDM)을 참조하십시오.
다음 코드 샘플에서는 연결에서 메타데이터 작업 영역을 가져온 다음 이 메타데이터 작업 영역을 사용하여 지정된 모델의 관계에 대한 정보를 검색하는 방법을 보여 줍니다. 메타데이터 작업 영역은 메타데이터 검색을 지원하는 런타임 서비스 구성 요소입니다.
코드 샘플에서는 CSpace를 사용하여 모델을 지정합니다. CSpace는 개념적 모델의 기본 이름을 나타냅니다.
코드 샘플에는 GetAssociations, GetAssociationSets 및 GetOneAssociationSet 메서드가 포함되어 있습니다.
GetAssociations 메서드는 연결 컬렉션을 가져온 다음 컬렉션을 반복하여 연결 End 멤버에 액세스합니다.
GetAssociationSets 메서드는 엔터티 컨테이너 컬렉션을 가져온 다음 컬렉션을 반복하여 지정된 컨테이너에 있는 각 연결 집합을 가져옵니다. EDM에서 EntityContainer는 엔터티 집합 및 연결 집합의 논리적 그룹을 나타냅니다. EDM에서 엔터티 컨테이너가 정의되는 방법에 대한 자세한 내용은 엔터티 컨테이너(EDM)를 참조하십시오.
GetOneAssociationSet 메서드는 엔터티 컨테이너 컬렉션을 가져온 다음 컬렉션을 반복하면서 지정된 이름을 사용하여 한 연결 집합만을 가져옵니다.
다음 코드 샘플에서는 AdventureWorks 전체 모델(EDM) 항목에서 제공하는 Adventureworks 모델을 사용합니다. 응용 프로그램 구성 파일의 예를 보려면 AdventureWorks 개체 모델 사용(EDM)을 참조하십시오.
using System;
using System.Data;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;
using System.Collections.ObjectModel;
class GetRelationshipsExample
{
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 associations from the conceptual model.
GetAssociations(workspace, DataSpace.CSpace);
// Get association sets from the conceptual model.
GetAssociationSets(workspace, DataSpace.CSpace);
// Get one assoiation set by using the specified
// relationship name from the conceptual model.
string relationshipName = "FK_Employee_Contact_ContactID";
GetOneAssociationSet(
workspace, relationshipName, 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 GetAssociations(
MetadataWorkspace workspace, DataSpace model)
{
Console.WriteLine("***Get Associations =>");
// Get a collection of association types.
ReadOnlyCollection<AssociationType> associationTypes =
workspace.GetItems<AssociationType>(model);
// Iterate through the collection to get each association type.
foreach (AssociationType associationType in associationTypes)
{
Console.WriteLine("AssociationType Name: {0}, Namespace: {1}",
associationType.Name,
associationType.NamespaceName);
// Iterate through the collection to get
// each association end member.
foreach (AssociationEndMember end in
associationType.AssociationEndMembers)
{
Console.WriteLine(
"\t End Name: {0}, Type: {1}, Multiplicity: {2}",
end.Name,
end.TypeUsage.EdmType.Name,
end.RelationshipMultiplicity);
}
}
}
public static void GetAssociationSets(
MetadataWorkspace workspace, DataSpace model)
{
Console.WriteLine("***Get Association Sets =>");
// Get a collection of entity containers.
ReadOnlyCollection<EntityContainer> containers =
workspace.GetItems<EntityContainer>(model);
// Iterate through the collection to get each entity container.
foreach (EntityContainer container in containers)
{
// Iterate through the collection to get each entity set base.
foreach (EntitySetBase baseSet in container.BaseEntitySets)
{
// EntitySetBase is a super type for
// AssociationSet and EntitySet.
// Check if the current object is an instance of the
// AssociationSet.
if (baseSet is AssociationSet)
{
Console.WriteLine(
"AssociationSet Name: {0} , " +
"AssociationType Name: {1} ",
baseSet.Name, baseSet.ElementType.FullName);
AssociationSet associationSet =
baseSet as AssociationSet;
// Iterate through the collection to get
// each association end.
foreach (AssociationSetEnd end in
associationSet.AssociationSetEnds)
{
Console.WriteLine(
"EntitySet Name: {0} , Name: {1}, " +
"AssociationEndMember: {2} ",
end.EntitySet,
end.Name,
end.CorrespondingAssociationEndMember);
}
}
}
}
}
public static void GetOneAssociationSet(
MetadataWorkspace workspace, string relationshipName,
DataSpace model)
{
Console.WriteLine("***Get One AssociationSet =>");
// Get a collection of entity containers.
ReadOnlyCollection<EntityContainer> containers =
workspace.GetItems<EntityContainer>(model);
// Iterate through the collection to get each entity container.
foreach (EntityContainer container in containers)
{
RelationshipSet relationshipSet;
// Check if the relationship with the specified name exists
// or not.
if (container.TryGetRelationshipSetByName(
relationshipName, true, out relationshipSet))
{
AssociationSet associationSet =
relationshipSet as AssociationSet;
Console.WriteLine(
"AssociationSet Name: {0} , " +
"AssociationType Name: {1} ",
associationSet.Name,
associationSet.ElementType.FullName);
}
}
}
}
Imports System
Imports System.Collections.ObjectModel
Imports System.Data
Imports System.Data.EntityClient
Imports System.Data.Metadata.Edm
Class GetRelationshipsExample
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 associations from the conceptual model.
GetAssociations(workspace, DataSpace.CSpace)
' Get association sets from the conceptual model.
GetAssociationSets(workspace, DataSpace.CSpace)
' Get one assoiation set by using the specified
' relationship name from the conceptual model.
Dim relationshipName As String = _
"FK_Employee_Contact_ContactID"
GetOneAssociationSet( _
workspace, relationshipName, 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 GetAssociations( _
ByVal workspace As MetadataWorkspace, ByVal model As DataSpace)
Console.WriteLine("***Get Associations =>")
' Get a collection of association types.
Dim associationTypes As ReadOnlyCollection(Of AssociationType) = _
workspace.GetItems(Of AssociationType)(model)
' Iterate through the collection to get each association type.
Dim associationType As AssociationType
For Each associationType In associationTypes
Console.WriteLine("AssociationType Name: {0}, Namespace: {1}", _
associationType.Name, associationType.NamespaceName)
' Iterate through the collection to get
' each association end member.
Dim endMember As AssociationEndMember
For Each endMember In associationType.AssociationEndMembers
Console.WriteLine(ControlChars.Tab & _
" End Name: {0}, Type: {1}, Multiplicity: {2}", _
endMember.Name, endMember.TypeUsage.EdmType.Name, _
endMember.RelationshipMultiplicity)
Next
Next
End Sub
Public Shared Sub GetAssociationSets( _
ByVal workspace As MetadataWorkspace, ByVal model As DataSpace)
Console.WriteLine("***Get Association Sets =>")
' Get a collection of entity containers.
Dim containers As ReadOnlyCollection(Of EntityContainer) = _
workspace.GetItems(Of EntityContainer)(model)
Dim container As EntityContainer
' Iterate through the collection to get each entity container.
For Each container In containers
Dim baseSet As EntitySetBase
' Iterate through the collection to get each entity set base.
For Each baseSet In container.BaseEntitySets
' EntitySetBase is a super type for
' AssociationSet and EntitySet.
' Check if the current object is an instance of the
' AssociationSet.
If TypeOf baseSet Is AssociationSet Then
Console.WriteLine( _
"AssociationSet Name: {0} , AssociationType Name: {1} ", _
baseSet.Name, baseSet.ElementType.FullName)
Dim associationSet As AssociationSet = _
TryCast(baseSet, AssociationSet)
Dim end1 As AssociationSetEnd
' Iterate through the collection to get
' each association end.
For Each end1 In associationSet.AssociationSetEnds
Console.WriteLine( _
"EntitySet Name: {0} , Name: {1}, AssociationEndMember: {2} ", _
end1.EntitySet, end1.Name, _
end1.CorrespondingAssociationEndMember)
Next
End If
Next
Next
End Sub
Public Shared Sub GetOneAssociationSet( _
ByVal workspace As MetadataWorkspace, _
ByVal relationshipName As String, _
ByVal model As DataSpace)
Console.WriteLine("***Get One AssociationSet =>")
' Get a collection of entity containers.
Dim containers As ReadOnlyCollection(Of EntityContainer) = _
workspace.GetItems(Of EntityContainer)(model)
Dim container As EntityContainer
' Iterate through the collection to get each entity container.
For Each container In containers
Dim relationshipSet As RelationshipSet
relationshipSet = Nothing
' Check if the relationship with the specified name exists
' or not.
If container.TryGetRelationshipSetByName( _
relationshipName, True, relationshipSet) Then
Dim associationSet As AssociationSet = _
TryCast(relationshipSet, AssociationSet)
Console.WriteLine( _
"AssociationSet Name: {0} , AssociationType Name: {1} ", _
associationSet.Name, associationSet.ElementType.FullName)
End If
Next
End Sub
End Class