構造型 (メタデータ)
エンティティ データ モデル (EDM) では、構造型はメンバを持つ EDM 型です。メンバは、StructuralType クラスから派生した型の内容を定義します。StructuralType クラスには、EntityType、RelationshipType、ComplexType など、複数の派生型があります。
EntityType は、EDM における顧客や注文などの、最上位レベルの概念を表します。RelationshipType は、EDM におけるアソシエーションを表す、AssociationType の基本データ型です。EDM におけるエンティティおよびアソシエーションの詳細については、「エンティティ型 (EDM)」および「アソシエーション (EDM)」を参照してください。
ComplexType は、エンティティ型のようなプロパティのセットを持つ一方でキー プロパティを持たない型を表します。EDM における複合型の詳細については、「複合型 (EDM)」を参照してください。
EDM スキーマにおいて、EntityType、RelationshipType、および ComplexType は、サブ要素またはメンバを持ちます。たとえば、エンティティ型のプロパティはそのメンバです。同様に、リレーションシップの End もそのメンバです。それぞれのメンバは、宣言する型を持っており、NULL 値の許容制約や、既定値の割り当てが存在する場合もあります。
以降のセクションでは、メンバについて詳しく説明します。
メンバ
EDM では、メンバは、StructuralType クラスから派生した型の内容を定義します。たとえば、次に示すのは、「AdventureWorks の完全な概念スキーマ (EDM)」トピックで使用された AdventureWorks 概念スキーマ (.csdl) ファイルの XML 表現です。この XML 表現は、Department EntityType を定義しています。Department エンティティのメンバは、DepartmentID、Name、GroupName、ModifiedDate、および EmployeeDepartmentHistory です。
<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.Properties は、EdmProperty オブジェクトを保持するコレクションを返します。
同様に、RelationshipType には、RelationshipEndMember オブジェクトのコレクションを返す System.Data.Metadata.Edm.RelationshipType.RelationshipEndMembers プロパティがあります。EdmProperty と RelationshipEndMember はどちらも EdmMember から派生しています。
構造型のメンバを取得するために使用できるプロパティを次に示します。
System.Data.Metadata.Edm.EntityType.Members : EntityType オブジェクトのすべてのメンバの一覧を取得します。EntityType のメンバは、EntityType オブジェクトのプロパティ、ナビゲーション プロパティ、およびキー メンバを含むことができます。System.Data.Metadata.Edm.EntityType.Members プロパティは、StructuralType クラスから継承されます。EntityType のメンバを概念スキーマに指定する方法については、「EntityType 要素 (CSDL)」を参照してください。
System.Data.Metadata.Edm.EntityType.Properties : EntityType オブジェクトのプロパティの一覧を取得します。EntityType のプロパティを概念スキーマに指定する方法については、「エンティティ型 (EDM)」および「EntityType 要素 (CSDL)」を参照してください。
System.Data.Metadata.Edm.EntityType.NavigationProperties : EntityType オブジェクトのナビゲーション プロパティの一覧を取得します。EntityType のナビゲーション プロパティを概念スキーマに指定する方法については、「エンティティ型 (EDM)」および「EntityType 要素 (CSDL)」を参照してください。
System.Data.Metadata.Edm.EntityType.KeyMembers : EntityType オブジェクトのキー メンバの一覧を取得します。このプロパティは、EntityTypeBase クラスから継承されます。
System.Data.Metadata.Edm.RelationshipType.RelationshipEndMembers : RelationshipType オブジェクトの End メンバの一覧を取得します。
System.Data.Metadata.Edm.AssociationType.AssociationEndMembers : AssociationType オブジェクトの End メンバの一覧を取得します。AssociationType の End メンバを概念スキーマに指定する方法については、「Association 要素 (CSDL)」を参照してください。
System.Data.Metadata.Edm.ComplexType.Properties : ComplexType オブジェクトのプロパティの一覧を取得します。ComplexType のプロパティを概念スキーマに指定する方法については、「複合型 (EDM)」および「複合型を使用してモデルを定義する方法 (Entity Framework)」を参照してください。ComplexType クラスのコード サンプルでは、指定されたモデル内の複合型のプロパティを取得する方法を示します。
次のコード サンプルでは、接続からメタデータ ワークスペースを取得した後、そのメタデータ ワークスペースを使用して、指定されたモデルのエンティティおよびリレーションシップの型のメンバに関する情報を取得します。メタデータ ワークスペースは、メタデータの取得をサポートするランタイム サービス コンポーネントです。
このコード サンプルでは、CSpace を使用してモデルを指定します。CSpace は、概念モデルの既定の名前を表します。このコード サンプルでは、「AdventureWorks Complete Model (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