高级元数据工作区

本主题说明 MetadataWorkspace 类用于检索实体数据模型 (EDM) 中的元数据信息的高级用法。为此,首先必须创建 MetadataWorkspace 类的新实例。然后,您必须向该元数据工作区注册项集合。

项集合负责加载元数据,并使元数据可用于整个元数据工作区。有关项集合的更多信息,请参阅项集合(元数据)

System.Data.Metadata.Edm 命名空间提供了 RegisterItemCollection 方法,以向元数据工作区注册项集合。

RegisterItemCollection 方法可确保对于每个模型只注册一个项集合。每个项集合负责加载有关一个特定模型的元数据。有关使用 ADO.NET 实体框架 的典型应用程序中的模型的更多信息,请参见元数据工作区概述

本文档中的代码示例创建 MetadataWorkspace 类的一个新实例。接下来,代码示例创建和注册 EdmItemCollection 类的一个新实例,以加载有关概念 (.csdl) 模型的元数据;并创建和注册 StoreItemCollection 类的一个新实例,以加载有关存储 (.ssdl) 模型的元数据。

然后,代码示例演示如何使用元数据工作区以在指定模型中检索有关所有类型的信息。请注意,元数据工作区是一个为检索元数据提供支持的运行时服务组件。

该代码示例使用 CSpaceSSpace 指定模型。CSpace 表示概念模型的默认名称。SSpace 表示存储模型的默认名称。

若要运行以下代码示例,您的数据文件夹必须包含概念 (.csdl) 架构文件、存储 (.ssdl) 架构文件和映射 (.msl) 架构文件。还必须使用映射架构文件的名称设置 FileName 输入参数。本示例使用在 AdventureWorks 完整模型 (EDM) 中定义的 AdventureWorks 模型。

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

另请参见

概念

元数据工作区