Advanced Metadata Workspace

This topic explains the advanced usage of the MetadataWorkspace class to retrieve metadata information within the Entity Data Model (EDM). In order to do that, you must first create a new instance of the MetadataWorkspace class. Then you must register the item collections with that metadata workspace.

Item collections are responsible for loading metadata and making it available through the metadata workspace. For more information about item collections, see Item Collections (Metadata).

The System.Data.Metadata.Edm namespace provides the RegisterItemCollection method to register the item collections with the metadata workspace.

The RegisterItemCollection method makes sure that only a single item collection is registered for each model. Each item collection is responsible for loading metadata about a specific model. For more information about the models in a typical application that uses the ADO.NET Entity Framework, see Metadata Workspace Overview.

The code sample in this document creates a new instance of the MetadataWorkspace class. Next, the code sample creates and registers a new instance of the EdmItemCollection class to load metadata about the conceptual (.csdl) model, and a new instance of the StoreItemCollection class to load metadata about the storage (.ssdl) model.

Then, the code sample demonstrates how to use the metadata workspace to retrieve information about all the types in the specified model. Note that the metadata workspace is a runtime service component that provides support for retrieving metadata.

To run the following code sample, your data folder must contain the conceptual (.csdl), storage (.ssdl), and mapping (.msl) schema files. You must also set the FileName input parameter with the name of your mapping schema file. The sample uses AdventureWorks model that is defined in AdventureWorks Complete Model (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

See Also

Concepts

Metadata Workspace