Поделиться через


Simple Types (Metadata)

In the Entity Data Model (EDM), simple types consist of primitive types. For more information about the simple types in the EDM, see Simple Types (EDM).

ADO.NET provides a PrimitiveType, which is derived from the SimpleType, to describe the .NET Framework primitive types, EDM primitive types, and storage provider-specific primitive types. The ADO.NET metadata infrastructure handles the mappings between the primitive types in the object, conceptual, and storage models. The object and conceptual model primitive types always have one-to-one default matches. Storage model primitive types depend on the storage provider or database used.

Each storage provider defines its own primitive types. The ADO.NET metadata infrastructure requests the definitions of the primitive types from the storage provider during runtime. Each storage provider must declare its primitive types in an XML document called the provider manifest.

The provider manifest file includes the list of the provider primitive types, the mappings between the conceptual and storage model primitive types, and the promotion and conversion rules between the conceptual and storage model primitive types.

The following code sample demonstrates how to get a metadata workspace from the connection and then use that metadata workspace to retrieve information about a specific primitive type and all other primitive types in the specified model. Note that the metadata workspace is a runtime service component that provides support for retrieving metadata.

The code sample uses a CSpace and a SSpace to specify the models. The CSpace represents the default name for the conceptual model. The SSpace represents the default name for the storage model. The code sample uses the connection string that is provided in the application config file. For an example of the application config file, see Using the AdventureWorks Object Model (EDM).

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

class GetPrimitiveTypesExample
{
  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 primitive types from the conceptual model.
         GetPrimitiveTypes(workspace, DataSpace.CSpace);

         // Get primitive types from the storage model.
         GetPrimitiveTypes(workspace, DataSpace.SSpace);
      }
    }
    catch (MetadataException exceptionMetadata)
    {
        Console.WriteLine("MetadataException: {0}", 
                         exceptionMetadata.Message);
    }
    catch (System.Data.MappingException exceptionMapping)
    {
        Console.WriteLine("MappingException: {0}",
                         exceptionMapping.Message);
    }
  }

  private static void GetPrimitiveTypes(
       MetadataWorkspace workspace, DataSpace model)
  {
    // Get a collection of the primitive types.
    ReadOnlyCollection<PrimitiveType> primitiveTypes =
            workspace.GetPrimitiveTypes(model);

    // Iterate through the collection to get each primitive type.
    foreach (PrimitiveType prim in primitiveTypes)
    {
       Console.WriteLine(
          "Type BuiltInTypeKind: {0}, Type: {1}, Type in Model: {2} ",
          prim.BuiltInTypeKind, prim.ClrEquivalentType.FullName, 
          prim.FullName);

     }
  }
}
Imports System
Imports System.Data
Imports System.Data.EntityClient
Imports System.Collections.ObjectModel
Imports System.Data.Metadata.Edm

Class GetPrimitiveTypesExample
   Shared Sub Main()
      Try
       ' 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")

         ' Open the conection.
         connection.Open()

         ' Access the metadata workspace.
         Dim workspace As MetadataWorkspace = _
            connection.GetMetadataWorkspace

         ' Get primitive types from the conceptual model.
         GetPrimitiveTypes(workspace, DataSpace.CSpace)

         ' Get primitive types from the storage model.
         GetPrimitiveTypes(workspace, DataSpace.SSpace)
       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

  Public Shared Sub GetPrimitiveTypes(ByVal workspace As _
            MetadataWorkspace, ByVal model As DataSpace)

    ' Get a collection of the primitive types.
    Dim primitiveTypes As ReadOnlyCollection(Of PrimitiveType) = _
       workspace.GetPrimitiveTypes(model)
    ' Iterate through the collection to get each primitive type.
     Dim prim As PrimitiveType
     For Each prim In primitiveTypes
       Console.WriteLine( _
         "Type BuiltInTypeKind: {0}, Type: {1}, Type in Model: {2} ", _
         prim.BuiltInTypeKind, prim.ClrEquivalentType.FullName, prim.FullName)
     Next
  End Sub
End Class

See Also

Concepts

Types (Metadata)
Metadata Type Hierarchy
Metadata Type Hierarchy Overview