实体连接和元数据工作区

当开发使用 ADO.NET 实体框架 的应用程序时,可以使用 EntityConnection 对象以连接到数据库。当您在 实体数据模型 (EDM) 中创建 EntityConnection 类的新实例时,可以在 app.config 文件中指定连接字符串。连接字符串引用包含所需模型和映射的一组元数据,同时还引用特定于存储的数据提供程序名称和连接字符串。

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

另请参见

概念

元数据工作区