Export (0) Print
Expand All

Types (Metadata)

.NET Framework 3.5

Types are the top-level constructs that form the basis of an Entity Data Model (EDM). ADO.NET provides the System.Data.Metadata.Edm namespace, which contains a set of types that represent concepts defined in the EDM in the Entity Framework. For more information about the models used in the Entity Framework, see Data Modeling in the Entity Framework and Metadata Workspace Overview.

As described in the Metadata Type Hierarchy Overview, an EdmType is a base class for the classes that represent types in the EDM. The top-level types such as the SimpleType, the StructuralType, the CollectionType, and the RefType, derive from the EdmType.

  • The SimpleType describes primitive types. For more information about simple types, see Simple Types (Metadata).

  • The StructuralType is a base type for all the types in the metadata type hierarchy that have members. For more information about structural types, see Structural Types (Metadata).

  • The CollectionType describes a collection of instances of a specific type.

  • The RefType holds the address of an entity for operations that use the entity.

The Entity Data Model Types topic also provides detailed information about how types are used in the EDM.

The following code samples demonstrate how to get a metadata workspace from the connection and then use that metadata workspace to retrieve information about a specific type and all other types in the specified model. The code samples use a CSpace and a SSpace to specify the model. The CSpace represents the default name for the conceptual model. The SSpace represents the default name for the storage model. Note that the metadata workspace is a runtime service component that provides support for retrieving metadata.

The code samples use the Adventureworks model that is provided in the AdventureWorks Complete Model (EDM) topic. For an example of the application config file, see Using the AdventureWorks Object Model (EDM).

// The first example:
using System;
using System.Data;
using System.Data.EntityClient;
using System.Collections.ObjectModel;
using System.Data.Metadata.Edm;

class GetTypeExample
{
  static void Main()
  {
    try
    {
      // 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"))
      {
        // Open the connection.
        connection.Open();

        // Access the metadata workspace.
        MetadataWorkspace workspace = 
              connection.GetMetadataWorkspace();

        // Get an EntityType object by using the specified type name, 
        // the namespace name, and the model. 
        EdmType departmentType1 = workspace.GetType(
              "Department", "AdventureWorksModel", DataSpace.CSpace);

        Console.WriteLine(
           "Type found in the conceptual model Name: {0}, {1} ",
           departmentType1.Name, 
           departmentType1.NamespaceName);

        // Get an EntityType object by using the specified type name, 
        // the namespace name, and the model. 
        EdmType departmentType2 = workspace.GetType( 
           "Department", "AdventureWorksModel.Store",
           DataSpace.SSpace);

        Console.WriteLine(
          "Type found in the storage model Name: {0}, {1} ",
          departmentType2.Name,
          departmentType2.NamespaceName);
      }
    }
    catch (MetadataException exceptionMetadata)
    {
      Console.WriteLine("MetadataException: {0}", 
                       exceptionMetadata.Message);
    }
    catch (System.Data.MappingException exceptionMapping)
    {
      Console.WriteLine("MappingException: {0}",
                       exceptionMapping.Message);
    }
  }
}
// The second example:
using System;
using System.Data;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;
using System.Collections.ObjectModel;

class GetTypesExample
{
  static void Main()
  {
    try
    {
       // 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"))
       {
          // Open the connection.
          connection.Open();

          // Access the metadata workspace.
          MetadataWorkspace workspace = 
               connection.GetMetadataWorkspace();

          // Get types from the conceptual model.
          GetTypesFromModel(workspace, DataSpace.CSpace);

          // Get types from the storage model.
          GetTypesFromModel(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 GetTypesFromModel(
    MetadataWorkspace workspace, DataSpace model)
  {
    // Get a collection of the EdmTypes. 
    // 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);

    // Iterate through the collection to get each edm type, 
    // specifically each entity type.
    foreach (EdmType item in types)
    {
       EntityType entityType = item as EntityType;
       if (entityType != null)
       {
          Console.WriteLine("Type: {0}, Type in Model: {1} ",
                item.GetType().FullName, item.FullName);
       }
     }
  }
}

In This Section

Structural Types (Metadata)

Describes the structural types and members.

Simple Types (Metadata)

Describes the simple types.

See Also

Community Additions

Show:
© 2014 Microsoft