项集合(元数据)

在 实体数据模型 (EDM) 中,项集合负责从持久性资源(如 XML 文件或公共语言运行库 (CLR) 程序集)中加载元数据,并且它们位于 MetadataWorkspace 类的实例中。

ADO.NET 提供 ItemCollection 类作为一个核心 API,用于加载和保留内存中的元数据。ItemCollection 具有多个派生类,如 ObjectItemCollectionEdmItemCollectionStoreItemCollectionStorageMappingItemCollection。其中的每个集合类都专门用于一种不同类型的元数据。以下各节说明这些集合类如何与不同类型的元数据进行交互。

ObjectItemCollection

ObjectItemCollection 类负责加载有关对象模型的元数据。对象模型表示可用作概念模型的编程实现的 CLR 类。

ObjectItemCollection 查找所引用的用 EdmSchemaAttribute 修饰的程序集,然后加载程序集中与 ADO.NET 实体框架 的持久类对应的类。尤其是加载自 System.Data.Objects.DataClasses 派生的类。

当引用的程序集中不存在类时,ADO.NET 元数据基础结构将隐式加载 实体框架 中的 CLR 元数据。例如,当您创建 ObjectQuery<T> 类的一个新实例以构造查询时,ADO.NET 元数据基础结构将检查元数据中是否已存在类型参数 <T>。如果元数据中没有类型参数 <T>,ADO.NET 元数据基础结构将从包含类型参数 <T> 的程序集中隐式加载元数据。

还可以从在当前应用程序未显式引用的特定程序集中加载元数据。在此情况下,您可以调用在 MetadataWorkspace 类中定义的 LoadFromAssembly 方法或在 ObjectItemCollection 类中定义的 LoadFromAssembly 方法。

LoadFromAssembly 方法在内部访问其已注册的 ObjectItemCollection 并调用在 ObjectItemCollection 类中定义的 LoadFromAssembly 方法。

以下代码示例演示如何从特定程序集中加载元数据。此代码示例使用 Adventureworks 模型以打开到基础数据库的连接。然后,它显式加载人力资源技能模型。有关 AdventureWorks 模型和人力资源技能模型的更多信息,请参见 AdventureWorks 完整模型 (EDM)人力资源技能 WinApp(EDM 示例应用程序)

Imports System
Imports System.Data
Imports System.Reflection
Imports System.Collections.ObjectModel
Imports System.Data.Metadata.Edm
Imports AdventureWorksModel

Class LoadAssemblyExample

  ' This sample illustrates loading metadata from a specific assembly.
  Public Shared Sub Main()
    Try
      ' Retrieve a MetadataWorkspace from an ObjectContext:
      ' AdventureWorksEntities 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

         ' Load metadata from the HRSkills assembly.
         workspace.LoadFromAssembly(Assembly.Load("HRSkills"))

         ' Get a collection of the EdmTypes from the object model.
         Dim types As ReadOnlyCollection(Of EdmType) = _
            workspace.GetItems(Of EdmType)(DataSpace.OSpace)

         ' Iterate through the collection to get each EdmType.
         Dim item As EdmType
         For Each item In types
             Console.WriteLine("Type: {0}, Type in Model: {1} ", _
                     item.GetType.FullName, item.FullName)
         Next
      End Using
    Catch exceptionMetadata As MetadataException
       Console.WriteLine("MetadataException: {0}", _
          exceptionMetadata.Message)
    End Try
  End Sub
End Class
using System;
using System.Data;
using System.Reflection;
using System.Collections.ObjectModel;
using System.Data.Metadata.Edm;
using AdventureWorksModel;

class LoadAssemblyExample
{
  // This sample illustrates loading metadata from a specific assembly.
  static void Main()
  {
     try
     {
        // Retrieve a MetadataWorkspace from an ObjectContext:
        // AdventureWorksEntities 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;

           // Load metadata from the HRSkills assembly.
           workspace.LoadFromAssembly(
                   Assembly.Load(@"HRSkills"));

           // Get a collection of the EdmTypes from the object model.
           ReadOnlyCollection<EdmType> types =
               workspace.GetItems<EdmType>(DataSpace.OSpace);

           // Iterate through the collection to get each EdmType.
           foreach (EdmType item in types)
           {
               Console.WriteLine("Type: {0}, Type in Model: {1} ",
                      item.GetType().FullName, item.FullName);
           }
       }
     }
     catch (MetadataException exceptionMetadata)
     {
        Console.WriteLine("MetadataException: {0}",
                         exceptionMetadata.Message);
     }
  }
}

EdmItemCollection

EdmItemCollection 类负责加载有关概念模型的元数据。EdmItemCollection 类从概念架构定义语言 (CSDL) 文件(这是概念模型的 XML 表示)加载其元数据。

可以从持久性源(如文件、程序集中的资源)或 XmlReader 创建 EdmItemCollectionStoreItemCollection

StoreItemCollection

StoreItemCollection 类负责加载有关存储(数据库)模型的元数据。StoreItemCollection 类从存储架构定义语言 (SSDL) 文件(这是存储模型的 XML 表示)加载其元数据。

可以从持久性源(如文件、程序集中的资源)或 XmlReader 创建 EdmItemCollectionStoreItemCollection

StorageMappingItemCollection

StorageMappingItemCollection 类负责加载表示概念模型与存储(数据库)模型之间映射的元数据。StorageMappingItemCollection 类从映射规范语言 (MSL) 文件(这是概念模型与存储模型之间映射的 XML 表示)加载其元数据。

另请参见

概念

元数据工作区