RelationshipManager Class


Used to track relationships between objects in the object context.

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


The example in this topic is based on the AdventureWorks Sales Model. The example adds new SalesOrderHeader entities to the Contact entity. 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 is used to track relationships between objects in an object context. 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. For more information, see Association Element (CSDL).



Creates a new RelationshipManager object.


Determines whether the specified object is equal to the current object.

(Inherited from Object)

Returns an enumeration of all the related ends managed by the relationship manager.


Serves as the default hash function.

(Inherited from Object)
GetRelatedCollection<TTargetEntity>(String, String)

Gets an EntityCollection<TEntity> of related objects with the specified relationship name and target role name.

GetRelatedEnd(String, String)

Returns either an EntityCollection<TEntity> or EntityReference<TEntity> of the correct type for the specified target role in a relationship.

GetRelatedReference<TTargetEntity>(String, String)

Gets the EntityReference<TEntity> for a related object by using the specified combination of relationship name and target role name.


Gets the Type of the current instance.

(Inherited from Object)
InitializeRelatedCollection<TTargetEntity>(String, String, EntityCollection<TTargetEntity>)

Initializes an existing EntityCollection<TEntity> that was created by using the parameterless constructor.

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

Initializes an existing EntityReference<TEntity> that was created by using the parameterless constructor.


Creates a shallow copy of the current Object.

(Inherited from Object)

Used internally to deserialize entity objects along with the RelationshipManager instances.


Called by Object Services to prepare an EntityKey for binary serialization with a serialized relationship.


Returns a string that represents the current object.

(Inherited from Object)

Applies to