EntityContainer 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示概念模型中的實體容器。 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
- 繼承
範例
下列程式碼範例會示範如何從連接中取得中繼資料工作空間,然後使用該中繼資料工作空間來擷取指定之模型內有關實體容器的資訊。 請注意,中繼資料工作空間是一個執行階段服務元件,它會提供用來擷取中繼資料的支援。
此程式碼範例會使用 CSpace 和 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 類別 (Class) 表示容器,這個容器會對應到儲存區中繼資料結構描述中的資料庫物件。 在儲存區層次上,EntityContainer 類別則表示資料表和/或索引鍵關聯性 (Relationship) 的描述,會針對根據該模型而建置的應用程式保存資料。 如需概念模型中實體容器的詳細資訊,請參閱 實體容器。
屬性
BaseEntitySets |
取得這個 EntityContainer 包含的實體集合和關聯集合的清單。 |
BuiltInTypeKind |
取得這個 EntityContainer 的內建型別類型。 |
Documentation |
取得或設定與這個型別相關聯的文件。 (繼承來源 MetadataItem) |
FunctionImports |
指定 EdmFunction 項目的集合。 每個函式都包含存在於資料庫中的預存程式詳細數據,或對應至實體及其屬性的對等 |
MetadataProperties |
取得目前型別的屬性清單。 (繼承來源 MetadataItem) |
Name |
取得這個 EntityContainer 的名稱。 |
方法
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetEntitySetByName(String, Boolean) |
使用實體集合的指定名稱傳回 EntitySet 物件。 |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetRelationshipSetByName(String, Boolean) |
使用關聯性 (Relationship) 集合的指定名稱傳回 RelationshipSet 物件。 |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
ToString() |
傳回這個 EntityContainer 的名稱。 |
TryGetEntitySetByName(String, Boolean, EntitySet) |
使用實體集合的指定名稱傳回 EntitySet 物件。 |
TryGetRelationshipSetByName(String, Boolean, RelationshipSet) |
使用關聯性 (Relationship) 集合的指定名稱傳回 RelationshipSet 物件。 |