Freigeben über


Erweiterter Metadaten-Arbeitsbereich

In diesem Thema wird die fortgeschrittene Verwendung der MetadataWorkspace-Klasse zum Abrufen von Metadateninformationen innerhalb des Entity Data Model (EDM) erläutert. Hierfür muss zunächst eine neue Instanz der MetadataWorkspace-Klasse erstellt werden. Anschließend müssen die Elementauflistungen in diesem Metadaten-Arbeitsbereich registriert werden.

Mit Elementauflistungen werden Metadaten geladen und für den Metadaten-Arbeitsbereich verfügbar gemacht. Weitere Informationen zu Elementauflistungen finden Sie unter Elementauflistungen (Metadaten).

Vom System.Data.Metadata.Edm-Namespace wird die RegisterItemCollection-Methode zum Registrieren der Elementauflistungen im Metadaten-Arbeitsbereich bereitgestellt.

Mithilfe der RegisterItemCollection-Methode wird sichergestellt, dass nur eine einzige Elementauflistung für jedes Modell registriert wird. Mit jeder Elementauflistung werden Metadaten zu einem bestimmten Modell geladen. Weitere Informationen zu den Modellen in einer typischen Anwendung, die ADO.NET Entity Framework verwendet, finden Sie unter Übersicht über den Metadaten-Arbeitsbereich.

Im Codebeispiel in diesem Dokument wird eine neue Instanz der MetadataWorkspace-Klasse erstellt. Anschließend wird im Codebeispiel eine neue Instanz der EdmItemCollection-Klasse erstellt und registriert, um Metadaten über das konzeptionelle Modell (CSDL) zu laden. Des Weiteren wird eine neue Instanz der StoreItemCollection-Klasse erstellt und registriert, um Metadaten über das Speichermodell (SSDL) zu laden.

Anschließend wird im Codebeispiel gezeigt, wie mit dem Metadaten-Arbeitsbereich Informationen über alle Typen im angegebenen Modell abgerufen werden können. Beachten Sie, dass es sich bei dem Metadaten-Arbeitsbereich um eine Laufzeitdienstkomponente handelt, die Unterstützung für das Abrufen von Metadaten bereitstellt.

Das Codebeispiel verwendet CSpace und SSpace, um die Modelle anzugeben. CSpace stellt den Standardnamen für das konzeptionelle Modell dar. SSpace stellt den Standardnamen für das Speichermodell dar.

Der Datenordner muss Schemadateien für Konzept (CSDL), Speicherung (SSDL) und Mapping (MSL) enthalten, damit das folgende Codebeispiel ausgeführt werden kann. Des Weiteren muss der FileName-Eingabeparameter auf den Namen der Mappingschema-Datei festgelegt werden. Im Beispiel wird das im Thema Das vollständige AdventureWorks-Modell (EDM) definierte "AdventureWorks"-Modell verwendet.

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

Siehe auch

Konzepte

Metadaten-Arbeitsbereich