共用方式為


進階中繼資料工作空間

本主題將說明 MetadataWorkspace 類別 (Class) 的進階用法,以擷取 Entity Data Model (EDM) 內的中繼資料資訊。為了要進行這項處理,您必須先建立 MetadataWorkspace 類別的新執行個體。然後,您必須向該中繼資料工作空間註冊項目集合。

項目集合會負責載入中繼資料,並透過中繼資料工作空間來使用該中繼資料。如需項目集合的詳細資訊,請參閱項目集合 (中繼資料)

System.Data.Metadata.Edm 命名空間提供了 RegisterItemCollection 方法,可向中繼資料工作空間註冊項目集合。

RegisterItemCollection 方法會確定每一個模型只註冊單一個項目集合。每一個項目集合都要負責載入有關特定模型的中繼資料。如需使用 ADO.NET 實體架構 之一般應用程式中模型的詳細資訊,請參閱 中繼資料工作空間概觀

本文件的程式碼範例會建立 MetadataWorkspace 類別的新執行個體 (Instance)。接下來,此程式碼範例會建立及註冊 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

另請參閱

概念

中繼資料工作空間