共用方式為


簡單型別 (中繼資料)

在 實體資料模型 (EDM) 中,簡單型別是由基本型別 (Primitive Type) 所組成。如需 EDM 中簡單型別的詳細資訊,請參閱簡單型別 (EDM)

ADO.NET 提供了衍生自 SimpleTypePrimitiveType,以描述 .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

另請參閱

概念

型別 (中繼資料)
中繼資料型別階層
中繼資料型別階層概觀