共用方式為


實體連接和中繼資料工作空間

當您開發使用 ADO.NET 實體架構 的應用程式時,可以使用 EntityConnection 物件來連接至資料庫。當您在 實體資料模型 (EDM) 中建立 EntityConnection 類別 (Class) 的新執行個體 (Instance) 時,可以在 app.config 檔案中指定連接字串 (Connection String)。此連接字串會參考一組中繼資料 (Metadata),其中包含必要的模型和對應,以及儲存體特有的資料提供者 (Data Provider) 名稱和連接字串。

EntityConnection 的建構函式包含 EntityConnectionEntityConnectionEntityConnection 採用的連接字串會參考單一參數中的中繼資料和資料庫連接。EntityConnection 建構函式則採用現有的 MetadataWorkspace 物件和資料庫連接字串。這樣就可以達成其重要案例:依據磁碟檔案以外的其他來源或是做為組件內嵌資源的中繼資料來建立 EntityConnection

每個 EntityConnection 執行個體都會保存 MetadataWorkspace 的參考,其中描述有關概念和儲存體模型的中繼資料,以及這兩個模型之間的對應。當您變更連接字串時,就會建立包含適當中繼資料的新中繼資料工作空間。

Note附註

連接字串中的任何變更會建立其他的中繼資料工作空間執行個體,因為中繼資料快取是以成品路徑、提供者名稱和提供者連接字串為索引鍵的。如果提供者連接字串不一樣,快取項目也就不同。

您可以從 EntityConnection 類別的執行個體或 ObjectContext 類別的特殊化版本中擷取 MetadataWorkspace 類別的執行個體。ObjectContext 類別會封裝 ADO.NET 與資料庫之間的連接,當做建立、讀取、更新和刪除作業的閘道。當您使用 ADO.NET 實體架構 來建立應用程式時,實體架構 就會自動產生衍生自 ObjectContext 的類別。

下列程式碼範例將示範兩種擷取 MetadataWorkspace 物件的方式。此範例中的第一個程式碼範例會明確地從 EntityConnection 類別的執行個體中擷取 MetadataWorkspace 類別的執行個體。此範例中的第二個程式碼範例會隱含地從 ObjectContext 類別的特殊化版本中擷取 MetadataWorkspace 類別的執行個體。此範例程式碼會使用 AdventureWorks 完整模型 (EDM) 中所描述的 Adventureworks 模型。如需應用程式組態檔的範例,請參閱使用 AdventureWorks 物件模型 (EDM)

using System;
using System.Data;
using System.Data.Objects;
using System.Collections.ObjectModel;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;
using AdventureWorksModel;

class MetadataWorkspaceExample
{
  static void Main()
  {
    try
    {
       // Retrieve a MetadataWorkspace from the EntityConnection:
       // The input parameter for an EntityConnection is a 
       // connection string that refers to the location of the 
       // XML files containing the persistent representation of 
       // metadata.
       // Establish a connection to the underlying data provider by
       // using the connection string specified in the config file.
       using (EntityConnection connection = 
             new EntityConnection("Name=AdventureWorksEntities"))
       {
          // Get the metadata workspace from the connection.
          MetadataWorkspace workspace = 
                     connection.GetMetadataWorkspace();

          // Get a collection of the entity containers.
          ReadOnlyCollection<EntityContainer> containers = 
                   workspace.GetItems<EntityContainer>(
                                      DataSpace.CSpace);

          // Iterate through the collection to get 
          // each entity container.
          foreach (EntityContainer container in containers)
          {
              Console.WriteLine("EntityContainer Name: {0} ", 
                               container.Name);
          }
       }
     }
     catch (MetadataException exceptionMetadata)
     {
        Console.WriteLine("MetadataException: {0}", 
                         exceptionMetadata.Message);
     }
     catch (System.Data.MappingException exceptionMapping)
     {
        Console.WriteLine("MappingException: {0}",
                         exceptionMapping.Message);
     }

     try
     {
        // Retrieve a MetadataWorkspace from an ObjectContext:
        // AdventureworksContext represents the ADO.NET ObjectContext 
        // that acts as a factory for queries; 
        // tracks object state; and is used to initiate updates 
        // against the database.
        using (
          AdventureWorksEntities db = new AdventureWorksEntities ())
        {
           // Dereference the workspace from the AdventureWorksEntities 
           // class (an ObjectContext specialization).
           MetadataWorkspace workspace = db.MetadataWorkspace;

           // Get a collection of the entity containers.
           ReadOnlyCollection<EntityContainer> containers = 
                    workspace.GetItems<EntityContainer>(
                                    DataSpace.CSpace);

           // Iterate through the collection to get 
           // each entity container.
           foreach (EntityContainer container in containers)
           {
               Console.WriteLine(
                    "EntityContainer Name: {0} ", 
                    container.Name);
            }
        }
     }
     catch (MetadataException exceptionMetadata)
     {
        Console.WriteLine("MetadataException: {0}", 
                         exceptionMetadata.Message);
     }
     catch (System.Data.MappingException exceptionMapping)
     {
        Console.WriteLine("MappingException: {0}",
                         exceptionMapping.Message);
     }
  }
}
Imports System
Imports System.Data
Imports System.Collections.ObjectModel
Imports System.Data.EntityClient
Imports System.Data.Metadata.Edm
Imports AdventureWorksModel

Class MetadataWorkspaceExample

  Public Shared Sub Main()
   Try
     ' Retrieve a MetadataWorkspace from the EntityConnection:
     ' The input parameter for an EntityConnection is a 
     ' connection string that refers to the location of the 
     ' XML files containing the persistent representation of 
     ' metadata.
     ' Establish a connection to the underlying data provider by
     ' using the connection string specified in the config file.
     Using connection As EntityConnection = _
        New EntityConnection("Name=AdventureWorksEntities")

        ' Get the metadata workspace from the connection.
        Dim workspace As MetadataWorkspace = _
           connection.GetMetadataWorkspace()

        ' Get a collection of the entity containers.
        Dim containers As ReadOnlyCollection(Of EntityContainer)
        containers = _
           workspace.GetItems(Of EntityContainer)(DataSpace.CSpace)

        ' Iterate through the collection to get 
        ' each entity container.
        Dim container As EntityContainer
        For Each container In containers
          Console.WriteLine("EntityContainer Name: {0} ", _
              container.Name)
        Next
     End Using
   Catch exceptionMetadata As MetadataException
     Console.WriteLine("MetadataException: {0}", _
        exceptionMetadata.Message)
   Catch exceptionMapping As MappingException
     Console.WriteLine("MappingException: {0}", _
          exceptionMapping.Message)
   End Try

   Try
     ' Retrieve a MetadataWorkspace from an ObjectContext:
    ' AdventureworksContext represents the ADO.NET ObjectContext that 
    ' acts as a factory for queries; 
    ' tracks object state; and is used to initiate updates 
    ' against the database.
    Using db As AdventureWorksEntities = New AdventureWorksEntities
      ' Dereference the workspace from the AdventureWorksEntities class
      ' (an ObjectContext specialization).
      Dim workspace As MetadataWorkspace = db.MetadataWorkspace

      ' Get a collection of the entity containers.
      Dim containers As ReadOnlyCollection(Of EntityContainer)
      containers = _
         workspace.GetItems(Of EntityContainer)(DataSpace.CSpace)

      ' Iterate through the collection to get 
      ' each entity container.
      Dim container As EntityContainer
      For Each container In containers
        Console.WriteLine("EntityContainer Name: {0} ", container.Name)
      Next
    End Using
     Catch exceptionMetadata As MetadataException
       Console.WriteLine("MetadataException: {0}", _
           exceptionMetadata.Message)
     Catch exceptionMapping As MappingException
        Console.WriteLine("MappingException: {0}", _
           exceptionMapping.Message)
     End Try
  End Sub
End Class

另請參閱

概念

中繼資料工作空間