简单类型(元数据)

在实体数据模型 (EDM) 中,简单类型由基元类型组成。有关 EDM 中的简单类型的更多信息,请参见简单类型 (EDM)

ADO.NET 提供了 PrimitiveType(从 SimpleType 派生),以描述 .NET Framework 基元类型、EDM 基元类型和存储提供程序特定的基元类型。ADO.NET 元数据基础结构处理对象模型、概念模型和存储模型中的各基元类型之间的映射。对象模型和概念模型基元类型始终具有一对一默认匹配。存储模型基元类型依赖于所使用的存储提供程序或数据库。

每个存储提供程序都定义其自己的基元类型。ADO.NET 元数据基础结构在运行时从存储提供程序请求基元类型的定义。每个存储提供程序都必须在称为提供程序清单的 XML 文档中声明其基元类型。

提供程序清单文件包含提供程序基元类型的列表、概念模型基元类型与存储模型基元类型之间的映射以及概念模型基元类型与存储模型基元类型之间的提升和转换规则。

下面的代码示例演示如何通过连接获取元数据工作区,然后使用该元数据工作区在指定模型中检索有关特定基元类型和所有其他基元类型的信息。请注意,元数据工作区是一个为检索元数据提供支持的运行时服务组件。

代码实例使用 CSpaceSSpace 以指定模型。CSpace 表示概念模型的默认名称。SSpace 表示存储模型的默认名称。代码示例使用在应用程序配置文件中提供的连接字符串。有关应用程序配置文件的示例,请参见使用 AdventureWorks 对象模型 (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

另请参见

概念

类型(元数据)
元数据类型层次结构
元数据类型层次结构概述