RelationshipManager 類別


用來追蹤物件內容中物件之間的關聯性 (Relationship)。Used to track relationships between objects in the object context.

public ref class RelationshipManager
public class RelationshipManager
type RelationshipManager = class
Public Class RelationshipManager


本主題中的範例是以 Microsoft SQL Server 產品範例: Database為基礎。The example in this topic is based on the Microsoft SQL Server Product Samples: Database. 此範例會將新 SalesOrderHeader 的實體新增至 Contact 實體。The example adds new SalesOrderHeader entities to the Contact entity. 然後,它會從實體取得所有相關端, Contact 並且顯示每個相關端的關聯性名稱、來源角色名稱和目標角色名稱。Then it gets all related ends from the Contact entity and displays the relationship name, source role name, and target role name for each related end.

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
    Contact contact = new Contact();

    // Create a new SalesOrderHeader.
    SalesOrderHeader newSalesOrder1 = new SalesOrderHeader();
    // Add SalesOrderHeader to the Contact.

    // Create another SalesOrderHeader.
    SalesOrderHeader newSalesOrder2 = new SalesOrderHeader();
    // Add SalesOrderHeader to the Contact.

    // Get all related ends
    IEnumerable<IRelatedEnd> relEnds =

    foreach (IRelatedEnd relEnd in relEnds)
        Console.WriteLine("Relationship Name: {0}", relEnd.RelationshipName);
        Console.WriteLine("Source Role Name: {0}", relEnd.SourceRoleName);
        Console.WriteLine("Target Role Name: {0}", relEnd.TargetRoleName);
Using context As New AdventureWorksEntities()
    Dim contact As New Contact()

    ' Create a new SalesOrderHeader. 
    Dim newSalesOrder1 As New SalesOrderHeader()
    ' Add SalesOrderHeader to the Contact. 

    ' Create another SalesOrderHeader. 
    Dim newSalesOrder2 As New SalesOrderHeader()
    ' Add SalesOrderHeader to the Contact. 

    ' Get all related ends 
    Dim relEnds As IEnumerable(Of IRelatedEnd) = DirectCast(contact, IEntityWithRelationships).RelationshipManager.GetAllRelatedEnds()

    For Each relEnd As IRelatedEnd In relEnds
        Console.WriteLine("Relationship Name: {0}", relEnd.RelationshipName)
        Console.WriteLine("Source Role Name: {0}", relEnd.SourceRoleName)
        Console.WriteLine("Target Role Name: {0}", relEnd.TargetRoleName)
End Using


RelationshipManager 是用來追蹤物件內容中物件之間的關聯性。RelationshipManager is used to track relationships between objects in an object context. 在物件內容中追蹤的每個關聯性都有一個 RelationshipManager 類別 (Class) 的執行個體 (Instance)。There is one instance of the RelationshipManager class for each relationship tracked in the object context. 關聯性是由概念模型中的關聯所定義。Relationships are defined by associations in a conceptual model. 如需詳細資訊,請參閱 Association 項目 (CSDL)For more information, see Association Element (CSDL).



建立新的 RelationshipManager 物件。Creates a new RelationshipManager object.


判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)

傳回關聯性管理員所管理之所有相關端的列舉型別 (Enumeration)。Returns an enumeration of all the related ends managed by the relationship manager.


做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetRelatedCollection<TTargetEntity>(String, String)

取得具有指定之關聯性名稱和目標角色名稱的相關物件的 EntityCollection<TEntity>Gets an EntityCollection<TEntity> of related objects with the specified relationship name and target role name.

GetRelatedEnd(String, String)

針對關聯性中的指定目標角色,傳回正確類型的 EntityCollection<TEntity>EntityReference<TEntity>Returns either an EntityCollection<TEntity> or EntityReference<TEntity> of the correct type for the specified target role in a relationship.

GetRelatedReference<TTargetEntity>(String, String)

使用關聯性名稱和目標角色名稱的指定組合,取得相關物件的 EntityReference<TEntity>Gets the EntityReference<TEntity> for a related object by using the specified combination of relationship name and target role name.


取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
InitializeRelatedCollection<TTargetEntity>(String, String, EntityCollection<TTargetEntity>)

初始化使用無參數建構函式所建立的現有 EntityCollection<TEntity>Initializes an existing EntityCollection<TEntity> that was created by using the parameterless constructor.

InitializeRelatedReference<TTargetEntity>(String, String, EntityReference<TTargetEntity>)

初始化使用無參數建構函式所建立的現有 EntityReference<TEntity>Initializes an existing EntityReference<TEntity> that was created by using the parameterless constructor.


建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)

在內部搭配 RelationshipManager 執行個體用來還原序列化實體物件。Used internally to deserialize entity objects along with the RelationshipManager instances.


由物件服務呼叫,以便針對含有序列化關聯性的二進位序列化 (Serialization) 準備 EntityKeyCalled by Object Services to prepare an EntityKey for binary serialization with a serialized relationship.


傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)