型別 (中繼資料)
型別是形成 實體資料模型 (EDM) 之基礎的最上層建構。ADO.NET 提供了包含一組型別的 System.Data.Metadata.Edm 命名空間,這一組型別代表 實體架構 的 EDM 中所定義的概念。如需 實體架構 中所用之模型的詳細資訊,請參閱 Entity Framework 中的資料模型化和 中繼資料工作空間概觀。
如同中繼資料型別階層概觀中所述,EdmType 是代表 EDM 型別之類別的基底類別。最上層型別 (如 SimpleType、StructuralType、CollectionType 和 RefType) 是衍生自 EdmType。
SimpleType 會描述基本型別 (Primitive Type)。如需簡單型別的詳細資訊,請參閱簡單型別 (中繼資料)。
StructuralType 是具有成員之中繼資料型別階層內所有型別的基底類型。如需結構化型別的詳細資訊,請參閱結構化型別 (中繼資料)。
CollectionType 會描述特定型別之執行個體的集合。
RefType 會針對使用實體的作業保存該實體的位址。
Entity Data Model 類型主題也提供了有關如何在 EDM 中使用型別的詳細資訊。
下列程式碼範例會示範如何從連接中取得中繼資料工作空間,然後使用該中繼資料工作空間來擷取指定之模型內有關特定型別和所有其他型別的資訊。此程式碼範例會使用 CSpace 和 SSpace 來指定此模型。CSpace 代表概念模型的預設名稱。SSpace 代表儲存體模型的預設名稱。請注意,中繼資料工作空間是一個執行階段服務元件,它會提供用來擷取中繼資料的支援。
此程式碼範例會使用 AdventureWorks 完整模型 (EDM) 主題所提供的 Adventureworks 模型。如需應用程式組態檔的範例,請參閱使用 AdventureWorks 物件模型 (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);
}
}
}
}
' The first example:
Imports System
Imports System.Data
Imports System.Data.EntityClient
Imports System.Data.Metadata.Edm
Class GetTypeExample
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 an EntityType object by using the specified type name,
' the namespace name, and the model.
Dim departmentType1 As EdmType = _
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.
Dim departmentType2 As EdmType = _
workspace.GetType("Department", _
"AdventureWorksModel.Store", _
DataSpace.SSpace)
Console.WriteLine( _
"Type found in the storage model Name: {0}, {1} ", _
departmentType2.Name, _
departmentType2.NamespaceName)
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
End Class
' The second example:
Imports System
Imports System.Collections.ObjectModel
Imports System.Data
Imports System.Data.EntityClient
Imports System.Data.Metadata.Edm
Class GetTypesExample
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 types from the conceptual model.
GetTypesFromModel(workspace, DataSpace.CSpace)
' Get types from the storage model.
GetTypesFromModel(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 GetTypesFromModel( _
ByVal workspace As MetadataWorkspace, ByVal model As DataSpace)
' 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).
Dim types As ReadOnlyCollection(Of EdmType) = _
workspace.GetItems(Of EdmType)(model)
Dim item As EdmType
' Iterate through the collection to get each edm type,
' specifically each entity type.
For Each item In types
Dim entityType As EntityType = TryCast(item, EntityType)
If (Not entityType Is Nothing) Then
Console.WriteLine("Type: {0}, Type in Model: {1} ", _
item.GetType.FullName, item.FullName)
End If
Next
End Sub
End Class
本章節內容
- 結構化型別 (中繼資料)
描述結構化型別和成員。
- 簡單型別 (中繼資料)
描述簡單型別。