EntityContainer 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在概念模型中表示實體容器。 An EntityContainer 是實體集合與關聯集合的邏輯群組。
public ref class EntityContainer sealed : System::Data::Metadata::Edm::GlobalItem
public sealed class EntityContainer : System.Data.Metadata.Edm.GlobalItem
type EntityContainer = class
inherit GlobalItem
Public NotInheritable Class EntityContainer
Inherits GlobalItem
- 繼承
範例
以下程式碼範例示範如何從連線取得一個元資料工作空間,然後利用該元資料工作空間取得指定資料模型中實體容器的資訊。 請注意,元資料工作區是一個執行時服務元件,提供支援元資料擷取的支援。
程式碼範例使用 a CSpace 和 a SSpace 來指定模型。 代表 CSpace 概念模型的預設名稱。 代表 SSpace 儲存模型的預設名稱。
該 GetEntityContainers 方法會取得一組實體容器,然後遍歷該集合,取得指定容器中的每個實體集合和關聯集合。 程式碼範例 採用 AdventureWorks 模型。
using System;
using System.Data;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;
using System.Collections.ObjectModel;
class GetEntityContainerExample
{
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 the entity containers in the conceptual model.
GetEntityContainers(workspace, DataSpace.CSpace);
// Get the entity containers in the storage model.
GetEntityContainers(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 GetEntityContainers(
MetadataWorkspace workspace, DataSpace model)
{
// Get a collection of the entity containers.
ReadOnlyCollection<EntityContainer> containers =
workspace.GetItems<EntityContainer>(model);
// Iterate through the collection to get each entity container.
foreach (EntityContainer container in containers)
{
Console.WriteLine("EntityContainer Name: {0} ",
container.Name);
// EntitySetBase is a super type for
// EntitySets and RelationshipSets.
// Iterate through the collection to get each EntitySetBase.
foreach (EntitySetBase baseSet in container.BaseEntitySets)
{
// Check if this instance is an EntitySet.
if (baseSet is EntitySet)
{
Console.WriteLine(
" EntitySet Name: {0} , EntityType Name: {1} ",
baseSet.Name, baseSet.ElementType.FullName);
}
// RelationshipSet is a super type for AssociationSet.
// Check if this instance is an AssociationSet.
if (baseSet is AssociationSet)
{
Console.WriteLine(
" AssociationSet Name: {0} , " +
"AssociationType Name: {1} ",
baseSet.Name, baseSet.ElementType.FullName);
// Get the AssociationSet.
AssociationSet associationSet =
baseSet as AssociationSet;
// Iterate through the collection to get
// each AssociatedSetEnd.
foreach (AssociationSetEnd end in
associationSet.AssociationSetEnds)
{
Console.WriteLine(
" EntitySet Name: {0} , Name: {1} ",
end.EntitySet, end.Name);
}
}
}
}
}
}
Imports System
Imports System.Collections.ObjectModel
Imports System.Data
Imports System.Data.EntityClient
Imports System.Data.Metadata.Edm
Class GetEntityContainerExample
Public 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 connection.
connection.Open()
' Access the metadata workspace.
Dim workspace As MetadataWorkspace = _
connection.GetMetadataWorkspace
' Get the entity containers in the conceptual model.
GetEntityContainers(workspace, DataSpace.CSpace)
' Get the entity containers in the storage model.
GetEntityContainers(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 GetEntityContainers( _
ByVal workspace As MetadataWorkspace, ByVal model As DataSpace)
' Get a collection of the entity containers.
Dim containers As ReadOnlyCollection(Of EntityContainer) = _
workspace.GetItems(Of EntityContainer)(model)
' Iterate through the collection to get each entity container.
Dim container As EntityContainer
For Each container In containers
Console.WriteLine("EntityContainer Name: {0} ", container.Name)
' EntitySetBase is a super type for
' EntitySets and RelationshipSets.
' Iterate through the collection to get each EntitySetBase.
Dim baseSet As EntitySetBase
For Each baseSet In container.BaseEntitySets
' Check if this instance is an EntitySet.
If TypeOf baseSet Is EntitySet Then
Console.WriteLine( _
" EntitySet Name: {0} , EntityType Name: {1} ", _
baseSet.Name, baseSet.ElementType.FullName)
End If
' RelationshipSet is a super type for AssociationSet.
' Check if this instance is an AssociationSet.
If TypeOf baseSet Is AssociationSet Then
Console.WriteLine( _
" AssociationSet Name: {0} , " + _
"AssociationType Name: {1} ", _
baseSet.Name, baseSet.ElementType.FullName)
' Get the AssociationSet.
Dim associationSet As AssociationSet = _
TryCast(baseSet, AssociationSet)
' Iterate through the collection to get
' each AssociatedSetEnd.
Dim endMember As AssociationSetEnd
For Each endMember In associationSet.AssociationSetEnds
Console.WriteLine( _
" EntitySet Name: {0} , Name: {1} ", _
endMember.EntitySet, endMember.Name)
Next
End If
Next
Next
End Sub
End Class
備註
在概念層面,該 EntityContainer 類別代表一個容器,將映射到儲存中繼資料結構中的資料庫物件。 在儲存層級,類別 EntityContainer 代表對資料表及/或金鑰關係的描述,這些關係會持久化於基於該模型的應用程式中。 欲了解更多概念模型中實體容器的資訊,請參見 實體容器。
屬性
| 名稱 | Description |
|---|---|
| BaseEntitySets |
會獲得包含 EntityContainer 實體集合和關聯集合的清單。 |
| BuiltInTypeKind |
會選內建的那種。EntityContainer |
| Documentation |
取得或設定與此類型相關的文件。 (繼承來源 MetadataItem) |
| FunctionImports |
指定一組 EdmFunction 元素。 每個函式包含資料庫中存在的儲存程序細節,或等效 |
| MetadataProperties |
取得目前類型的屬性清單。 (繼承來源 MetadataItem) |
| Name |
知道這個 EntityContainer名字。 |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GetEntitySetByName(String, Boolean) |
透過使用指定的實體集合名稱回傳 EntitySet 物件。 |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetRelationshipSetByName(String, Boolean) |
透過指定名稱回傳 RelationshipSet 物件。 |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
回傳這個 EntityContainer名稱。 |
| TryGetEntitySetByName(String, Boolean, EntitySet) |
透過使用指定的實體集合名稱回傳 EntitySet 物件。 |
| TryGetRelationshipSetByName(String, Boolean, RelationshipSet) |
透過指定名稱回傳 RelationshipSet 物件。 |