Item Collections (Metadata)

In the Entity Data Model (EDM), item collections are responsible for loading metadata from persistent resources, such as XML files or common language runtime (CLR) assemblies, and are in an instance of the MetadataWorkspace class.

ADO.NET provides an ItemCollection class as a core API for loading and holding in-memory metadata. The ItemCollection has several derived classes, such as ObjectItemCollection, EdmItemCollection, StoreItemCollection, and StorageMappingItemCollection. Each of these collection classes is specialized for a different type of metadata. The following sections explain how these collection classes interact with different types of metadata.

ObjectItemCollection

The ObjectItemCollection class is responsible for loading metadata about the object model. The object model represents the CLR classes that can be used as a programmatic realization of the conceptual model.

The ObjectItemCollection looks for referenced assemblies that are decorated with the EdmSchemaAttribute and then loads the classes in the assemblies that correspond to persistent classes for the ADO.NET Entity Framework. Specifically, classes that derive from the System.Data.Objects.DataClasses are loaded.

When the classes do not exist in the referenced assemblies, the ADO.NET metadata infrastructure loads the CLR metadata in the Entity Framework implicitly. For example, when you create a new instance of the ObjectQuery<T> class to construct a query, the ADO.NET metadata infrastructure checks whether the type parameter <T> is already in the metadata or not. If the type parameter <T> is not in the metadata, the ADO.NET metadata infrastructure loads the metadata from the assembly that includes the type parameter <T> implicitly.

You can also load metadata from a specific assembly that is not explicitly referenced in your current application. In this case, you can invoke either the LoadFromAssembly method that is defined in the MetadataWorkspace class or LoadFromAssembly method that is defined in the ObjectItemCollection class.

The LoadFromAssembly method internally accesses its registered ObjectItemCollection and calls the LoadFromAssembly method that is defined on the ObjectItemCollection class.

The following code sample demonstrates how to load metadata from a specific assembly. The code sample uses the AdventureWorks model to open a connection to the underlying database. Then, it loads the Human Resources Skills model explicitly. For more information about AdventureWorks and Human Resources Skills models, see AdventureWorks Complete Model (EDM) and Human Resources Skills WinApp (EDM Sample Application).

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

An EdmItemCollection class is responsible for loading metadata about the conceptual model. The EdmItemCollection class loads its metadata from a conceptual schema definition language (CSDL) file that is an XML representation of the conceptual model.

EdmItemCollection and StoreItemCollection can be created from persistent sources like files, resources in assemblies, or from XmlReader.

StoreItemCollection

An StoreItemCollection class is responsible for loading the metadata about the storage (database) model. The StoreItemCollection class loads its metadata from a store schema definition language (SSDL) file that is an XML representation of the storage model.

EdmItemCollection and StoreItemCollection can be created from persistent sources like files, resources in assemblies, or from XmlReader.

StorageMappingItemCollection

A StorageMappingItemCollection class is responsible for loading metadata that represents the mapping between the conceptual model and the storage (database) model. The StorageMappingItemCollection class loads its metadata from a mapping specification language (MSL) file that is an XML representation of the mapping between the conceptual model and the storage model.

See Also

Concepts

Metadata Workspace