Condividi tramite


Area di lavoro metadati avanzata

In questo argomento viene descritto l'utilizzo avanzato della classe MetadataWorkspace per recuperare informazioni sui metadati in EDM (Entity Data Model). A questo scopo, è innanzitutto necessario creare una nuova istanza della classe MetadataWorkspace. È quindi necessario registrare gli insieme di elementi con l'area di lavoro metadati.

Gli insiemi di elementi sono responsabili del caricamento dei metadati e della disponibilità dei metadati in tutta l'area di lavoro metadati. Per ulteriori informazioni sugli insiemi di elementi, vedere Insiemi di elementi (metadati).

Lo spazio dei nomi System.Data.Metadata.Edm fornisce il metodo RegisterItemCollection per registrare gli insiemi di elementi con l'area di lavoro metadati.

Il metodo RegisterItemCollection garantisce che per ogni modello venga registrato solo un insieme di elementi. Ogni insieme di elementi è responsabile per il caricamento dei metadati relativi a un modello specifico. Per ulteriori informazioni sui modelli in un'applicazione tipica che utilizza ADO.NET Entity Framework, vedere Panoramica dell'area di lavoro metadati.

Nell'esempio di codice di questo documento viene creata una nuova istanza della classe MetadataWorkspace Viene quindi creata e registrata una nuova istanza della classe EdmItemCollection per caricare metadati relativi al modello concettuale (con estensione csdl), nonché una nuova istanza della classe StoreItemCollection per caricare metadati relativi al modello di archiviazione (con estensione ssdl).

Viene infine illustrato come utilizzare l'area di lavoro metadati per recuperare informazioni su tutti i tipi 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 vengono utilizzati CSpace e SSpace per specificare i modelli. CSpace rappresenta il nome predefinito per il modello concettuale. SSpace rappresenta il nome predefinito per il modello di archiviazione.

Per eseguire l'esempio di codice seguente, la cartella dati deve contenere i file dello schema concettuale (con estensione csdl), dello schema di archiviazione (con estensione ssdl) e dello schema di mapping (con estensione msl). È inoltre necessario impostare il parametro di input FileName con il nome del file dello schema di mapping. Nell'esempio viene utilizzato il modello AdventureWorks definito in Modello completo di AdventureWorks (EDM).

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.ObjectModel;
using System.Data.Metadata.Edm;

class AdvancedGetTypeExample
{
  // The data folder contains the EDM schemas such as 
  // conceptual schema file (.csdl),  
  // the storage schema file (.ssdl), 
  // and the mapping file (.msl).
  private const string 
    PathToDataFolder = @"..\..\..\..\Adventureworks\Adventureworks";

  private const string
    ConnectionString = @"server=serverName;database=Adventureworks;Integrated Security=true";

  static void Main()
  {
     try
     {
       // Construct a new instance of the metadata workspace.
       MetadataWorkspace workspace = new MetadataWorkspace();

       // Create a new item collection with the specified 
       // connection and the folder info 
       // where the entity data model (EDM) schemas exist.
       EdmItemCollection edmCollection =
              new EdmItemCollection(PathToDataFolder);

       // Register the item collection with the metadata workspace.
       workspace.RegisterItemCollection(edmCollection);

       // Create a new item collection with the specified 
       // connection and the folder info 
       // where the entity data model (EDM) schemas exist.
       // SqlConnection object is used to supply the types to 
       // the StoreItemCollection. 
       StoreItemCollection storeCollection =
                new StoreItemCollection(PathToDataFolder);

       // Register the item collection with the metadata workspace.
       workspace.RegisterItemCollection(storeCollection);            

       // Get items from the conceptual model.
       GetItemsFromModel(workspace, DataSpace.CSpace);

       // Get items from the storage model.
       GetItemsFromModel(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 GetItemsFromModel
    (MetadataWorkspace workspace, DataSpace model)
  {
    Console.WriteLine("Display items at the {0} model",
       model.ToString());
    // 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);

    // For the conceptual model (DataSpace.CSpace):
    // This collection contains all types defined in .csdl file and 
    // also 
    // the canonical functions and primitive types defined 
    // in the Entity Data Model (EDM).
    // For the storage model (DataSpace.SSpace):
    // This collection contains all types defined in .ssdl file 
    // and also
    // all SQL Server primitive types and functions.

    // Iterate through the collection to get each type.
    foreach (EdmType item in types)
    {
       Console.WriteLine("Type: {0}, Type in Model: {1} ",
               item.GetType().FullName, item.FullName);
    }
  }
}
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections.ObjectModel
Imports System.Data.Metadata.Edm

Class AdvancedGetTypeExample
   ' The data folder contains the EDM schemas such as 
   ' conceptual schema file (.csdl),  
   ' the storage schema file (.ssdl), 
   ' and the mapping file (.msl).
   Private Const PathToDataFolder As String = _
        "..\..\..\..\Adventureworks\Adventureworks"

   Private Const ConnectionString As String = _
    "server=serverName;database=Adventureworks;Integrated Security=true"

  Public Shared Sub Main()
    Try
      ' Construct a new instance of the metadata workspace.
      Dim workspace As MetadataWorkspace = New MetadataWorkspace()

      ' Create a new item collection with the specified 
      ' connection and the folder info 
      ' where the entity data model (EDM) schemas exist.
      Dim edmCollection As New EdmItemCollection(PathToDataFolder)

      ' Register the item collection with the metadata workspace.
      workspace.RegisterItemCollection(edmCollection)

      ' Create a new item collection with the specified 
      ' connection and the folder info 
      ' where the entity data model (EDM) schemas exist.
      ' SqlConnection object is used to supply the types to 
      ' the StoreItemCollection. 
      Dim storeCollection As New StoreItemCollection(PathToDataFolder)

      ' Register the item collection with the metadata workspace.
      workspace.RegisterItemCollection(storeCollection)

      ' Get items from the conceptual model.
      GetItemsFromModel(workspace, DataSpace.CSpace)

      ' Get items from the storage model.
      GetItemsFromModel(workspace, DataSpace.SSpace)
    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 GetItemsFromModel( _
       ByVal workspace As MetadataWorkspace, _
       ByVal model As DataSpace)
    
    Console.WriteLine("Display items at the {0} model", model.ToString)

    ' 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)

    ' For the conceptual model (DataSpace.CSpace):
    ' This collection contains all types defined in .csdl file and also 
    ' the canonical functions and primitive types defined 
    ' in the Entity Data Model (EDM).
    ' For the storage model (DataSpace.SSpace):
    ' This collection contains all types defined in .ssdl file and also
    ' all SQL Server primitive types and functions.

    ' Iterate through the collection to get each type.
    Dim item As EdmType
    For Each item In types
      Console.WriteLine( _
            "Type: {0}, Type in Model: {1} ", _
             item.GetType.FullName, item.FullName)
    Next
  End Sub
End Class

Vedere anche

Concetti

Area di lavoro metadati