Condividi tramite


Tipi (metadati)

I tipi sono i costrutti di livello superiore che formano la base di Entity Data Model (EDM). ADO.NET fornisce lo spazio dei nomi System.Data.Metadata.Edm, che contiene un insieme di tipi che rappresentano i concetti definiti in EDM in Entity Framework. Per ulteriori informazioni sui modelli utilizzati in Entity Framework, vedere Modellazione dei dati in Entity Framework e Panoramica dell'area di lavoro metadati.

Come descritto in Panoramica della gerarchia dei tipi di metadati, un oggetto EdmType è una classe di base per le classi che rappresentano i tipi in EDM. I tipi di livello superiore, come SimpleType, StructuralType, CollectionType e RefType, derivano da EdmType.

  • SimpleType descrive i tipi primitivi. Per ulteriori informazioni sui tipi semplici, vedere Tipi semplici (metadati).

  • StructuralType è un tipo di base per tutti i tipi nella gerarchia dei tipi di metadati che includono membri. Per ulteriori informazioni sui tipi strutturali, vedere Tipi strutturali (metadati).

  • CollectionType descrive un insieme di istanze di un tipo specifico.

  • RefType contiene l'indirizzo di un'entità per le operazioni che utilizzano l'entità.

L'argomento Tipi EDM (Entity Data Model) fornisce inoltre informazioni dettagliate sulle modalità di utilizzo dei tipi in EDM.

Negli esempi di codice seguenti viene illustrato come ottenere un'area di lavoro metadati dalla connessione e quindi utilizzarla per recuperare informazioni su un tipo specifico e su tutti gli altri tipi nel modello specificato. Negli esempi di codice vengono utilizzati un oggetto CSpace e un oggetto SSpace per specificare il modello. CSpace rappresenta il nome predefinito per il modello concettuale. SSpace rappresenta il nome predefinito per il modello di archiviazione. Si noti che l'area di lavoro metadati è un componente del servizio di runtime che fornisce supporto per il recupero dei metadati.

Negli esempi di codice viene utilizzato il modello AdventureWorks fornito 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).

// The first example:
using System;
using System.Data;
using System.Data.EntityClient;
using System.Collections.ObjectModel;
using System.Data.Metadata.Edm;

class GetTypeExample
{
  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 an EntityType object by using the specified type name, 
        // the namespace name, and the model. 
        EdmType departmentType1 = workspace.GetType(
              "Department", "AdventureWorksModel", DataSpace.CSpace);

        Console.WriteLine(
           "Type found in the conceptual model Name: {0}, {1} ",
           departmentType1.Name, 
           departmentType1.NamespaceName);

        // Get an EntityType object by using the specified type name, 
        // the namespace name, and the model. 
        EdmType departmentType2 = workspace.GetType( 
           "Department", "AdventureWorksModel.Store",
           DataSpace.SSpace);

        Console.WriteLine(
          "Type found in the storage model Name: {0}, {1} ",
          departmentType2.Name,
          departmentType2.NamespaceName);
      }
    }
    catch (MetadataException exceptionMetadata)
    {
      Console.WriteLine("MetadataException: {0}", 
                       exceptionMetadata.Message);
    }
    catch (System.Data.MappingException exceptionMapping)
    {
      Console.WriteLine("MappingException: {0}",
                       exceptionMapping.Message);
    }
  }
}
// The second example:
using System;
using System.Data;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;
using System.Collections.ObjectModel;

class GetTypesExample
{
  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 types from the conceptual model.
          GetTypesFromModel(workspace, DataSpace.CSpace);

          // Get types from the storage model.
          GetTypesFromModel(workspace, DataSpace.SSpace);
       }
    }
    catch (MetadataException exceptionMetadata)
    {
      Console.WriteLine("MetadataException: {0}", 
                       exceptionMetadata.Message);
    }
    catch (System.Data.MappingException exceptionMapping)
    {
       Console.WriteLine("MappingException: {0}",
                        exceptionMapping.Message);
    }
  }

  public static void GetTypesFromModel(
    MetadataWorkspace workspace, DataSpace model)
  {
    // Get a collection of the EdmTypes. 
    // An EdmType class is the base class for the classes 
    // that represent types in the Entity Data Model (EDM).
    ReadOnlyCollection<EdmType> types =
          workspace.GetItems<EdmType>(model);

    // Iterate through the collection to get each edm type, 
    // specifically each entity type.
    foreach (EdmType item in types)
    {
       EntityType entityType = item as EntityType;
       if (entityType != null)
       {
          Console.WriteLine("Type: {0}, Type in Model: {1} ",
                item.GetType().FullName, item.FullName);
       }
     }
  }
}
' The first example:
Imports System
Imports System.Data
Imports System.Data.EntityClient
Imports System.Data.Metadata.Edm

Class GetTypeExample
   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 an EntityType object by using the specified type name, 
           ' the namespace name, and the model. 
           Dim departmentType1 As EdmType = _
              workspace.GetType("Department", "AdventureWorksModel", _
              DataSpace.CSpace)
           Console.WriteLine( _
           "Type found in the conceptual model Name: {0}, {1} ", _
           departmentType1.Name, _
           departmentType1.NamespaceName)

           ' Get an EntityType object by using the specified type name, 
           ' the namespace name, and the model. 
           Dim departmentType2 As EdmType = _
               workspace.GetType("Department", _
               "AdventureWorksModel.Store", _
               DataSpace.SSpace)
           Console.WriteLine( _
              "Type found in the storage model Name: {0}, {1} ", _
              departmentType2.Name, _
              departmentType2.NamespaceName)
        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
End Class
' The second example:
Imports System
Imports System.Collections.ObjectModel
Imports System.Data
Imports System.Data.EntityClient
Imports System.Data.Metadata.Edm

Class GetTypesExample
   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 types from the conceptual model.
         GetTypesFromModel(workspace, DataSpace.CSpace)

         ' Get types from the storage model.
         GetTypesFromModel(workspace, DataSpace.SSpace)
       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 GetTypesFromModel( _
     ByVal workspace As MetadataWorkspace, ByVal model As DataSpace)
     ' Get a collection of the EdmTypes. 
     ' An EdmType class is the base class for the classes 
     ' that represent types in the Entity Data Model (EDM).
     Dim types As ReadOnlyCollection(Of EdmType) = _
         workspace.GetItems(Of EdmType)(model)
     Dim item As EdmType

     ' Iterate through the collection to get each edm type, 
     ' specifically each entity type.
     For Each item In types
       Dim entityType As EntityType = TryCast(item, EntityType)
       If (Not entityType Is Nothing) Then
         Console.WriteLine("Type: {0}, Type in Model: {1} ", _
             item.GetType.FullName, item.FullName)
        End If
     Next
   End Sub
End Class

Contenuto della sezione

Vedere anche

Concetti

Gerarchia dei tipi di metadati
Panoramica della gerarchia dei tipi di metadati
Tipi EDM (Entity Data Model)