Tipi strutturali (metadati)
In Entity Data Model (EDM) i tipi strutturali sono tipi EDM che includono membri. I membri definiscono il contenuto dei tipi derivati dalla classe StructuralType. La classe StructuralType ha diversi tipi derivati, ad esempio EntityType, RelationshipType e ComplexType.
EntityType rappresenta un concetto di livello superiore, ad esempio un cliente o un ordine in EDM. RelationshipType è un tipo di base per AssociationType, che rappresenta un'associazione in EDM. Per ulteriori informazioni sulle entità e le associazioni in EDM, vedere EntityType (EDM) e Associazione (EDM).
ComplexType rappresenta un tipo che include un insieme di proprietà come tipo di entità, ma non include una proprietà Key. Per ulteriori informazioni sui tipi complessi in EDM, vedere Tipo complesso (EDM).
Negli schemi EDM EntityType, RelationshipType e ComplexType includono sottoelementi o membri. Le proprietà di un tipo di entità, ad esempio, rappresentano i relativi membri. Analogamente, anche le parti di una relazione rappresentano i relativi membri. Ogni membro dispone di un tipo dichiarante e può disporre del vincolo Nullability o un'assegnazione di valore predefinito.
Nelle sezioni seguenti vengono fornite informazioni dettagliate sui membri.
Membri
In EDM i membri definiscono il contenuto dei tipi derivati dalla classe StructuralType. La rappresentazione XML seguente, ad esempio, deriva dal file dello schema concettuale (con estensione CSDL) di AdventureWorks incluso nell'argomento Schema concettuale completo di AdventureWorks (EDM). Questa rappresentazione XML definisce l'elemento EntityType dell'entità Department. I membri dell'entità Department sono DepartmentID, Name, GroupName, ModifiedDate e 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>
È possibile ottenere un elenco dei membri in qualsiasi tipo strutturale utilizzando la proprietà System.Data.Metadata.Edm.StructuralType.Members fornita da StructuralType. La proprietà Members restituisce un oggetto ReadOnlyMetadataCollection che contiene gli oggetti EdmMember.
I tipi che derivano da StructuralType archiviano i relativi membri nella proprietà Members ereditata da StructuralType.
Oltre alla proprietà Members ereditata da StructuralType, ogni tipo derivato fornisce alcune proprietà aggiuntive per ottenere l'elenco dei membri speciali che possono essere dichiarati nel tipo. System.Data.Metadata.Edm.EntityType.Properties, ad esempio, restituisce un insieme contenente gli oggetti EdmProperty.
Analogamente, RelationshipType contiene la proprietà System.Data.Metadata.Edm.RelationshipType.RelationshipEndMembers, che restituisce un insieme di oggetti RelationshipEndMember. Si noti che sia EdmProperty sia RelationshipEndMember derivano da EdmMember.
Nell'elenco seguente è incluso un insieme di proprietà che è possibile utilizzare per recuperare i membri dei tipi strutturali:
System.Data.Metadata.Edm.EntityType.Members: ottiene un elenco di tutti i membri dell'oggetto EntityType. I membri di EntityType possono includere proprietà, proprietà di navigazione e membri principali dell'oggetto EntityType. La proprietà System.Data.Metadata.Edm.EntityType.Members viene ereditata dalla classe StructuralType. Per ulteriori informazioni su come specificare i membri di EntityType in uno schema concettuale, vedere Elemento EntityType (CSDL).
System.Data.Metadata.Edm.EntityType.Properties: ottiene un elenco delle proprietà dell'oggetto EntityType. Per ulteriori informazioni su come specificare le proprietà di EntityType in uno schema concettuale, vedere EntityType (EDM) e Elemento EntityType (CSDL).
System.Data.Metadata.Edm.EntityType.NavigationProperties: ottiene un elenco delle proprietà di navigazione dell'oggetto EntityType. Per ulteriori informazioni su come specificare le proprietà di navigazione di EntityType in uno schema concettuale, vedere EntityType (EDM) e Elemento EntityType (CSDL).
System.Data.Metadata.Edm.EntityType.KeyMembers: ottiene un elenco di tutti i membri principali dell'oggetto EntityType. Questa proprietà viene ereditata dalla classe EntityTypeBase.
System.Data.Metadata.Edm.RelationshipType.RelationshipEndMembers: ottiene un elenco dei membri End dell'oggetto RelationshipType.
System.Data.Metadata.Edm.AssociationType.AssociationEndMembers: ottiene un elenco dei membri End dell'oggetto AssociationType. Per ulteriori informazioni su come specificare i membri End di AssociationType in uno schema concettuale, vedere Elemento Association (CSDL).
System.Data.Metadata.Edm.ComplexType.Properties: ottiene un elenco delle proprietà dell'oggetto ComplexType. Per ulteriori informazioni su come specificare le proprietà di ComplexType in uno schema concettuale, vedere Tipo complesso (EDM) e Procedura: definire un modello con un tipo complesso (Entity Framework). Nell'esempio di codice nella classe ComplexType viene illustrato il recupero delle proprietà dei tipi complessi nel modello specificato.
L'esempio di codice seguente ottiene un'area di lavoro metadati dalla connessione e la utilizza per recuperare informazioni sui membri dei tipi di entità e di relazione nel modello specificato. Si noti che l'area di lavoro metadati è un componente del servizio di runtime che fornisce supporto per il recupero dei metadati.
Nell'esempio di codice viene utilizzato un oggetto CSpace per specificare il modello. CSpace rappresenta il nome predefinito per il modello concettuale. Nell'esempio di codice viene utilizzato il modello AdventureWorks incluso nell'argomento Modello completo di AdventureWorks (EDM). Per un esempio del file di configurazione dell'applicazione, vedere Utilizzo del modello a oggetti di 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
Vedere anche
Concetti
Tipi (metadati)
Gerarchia dei tipi di metadati
Panoramica della gerarchia dei tipi di metadati