Export (0) Print
Expand All

RelationshipManager Class

Used to track relationships between objects in the object context.

System.Object
  System.Data.Objects.DataClasses.RelationshipManager

Namespace:  System.Data.Objects.DataClasses
Assembly:  System.Data.Entity (in System.Data.Entity.dll)

[SerializableAttribute]
public class RelationshipManager

The RelationshipManager type exposes the following members.

  NameDescription
Public methodStatic memberCreateCreates a new RelationshipManager object.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetAllRelatedEndsReturns an enumeration of all the related ends managed by the relationship manager.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetRelatedCollection<TTargetEntity>Gets an EntityCollection<TEntity> of related objects with the specified relationship name and target role name.
Public methodGetRelatedEndReturns either an EntityCollection<TEntity> or EntityReference<TEntity> of the correct type for the specified target role in a relationship.
Public methodGetRelatedReference<TTargetEntity>Gets the EntityReference<TEntity> for a related object by using the specified combination of relationship name and target role name.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitializeRelatedCollection<TTargetEntity>Initializes an existing EntityCollection<TEntity> that was created by using the default constructor.
Public methodInitializeRelatedReference<TTargetEntity>Initializes an existing EntityReference<TEntity> that was created by using the default constructor.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodOnDeserializedInfrastructure. Used internally to deserialize entity objects along with the RelationshipManager instances.
Public methodOnSerializingCalled by Object Services to prepare an EntityKey for binary serialization with a serialized relationship.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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 (EDM).

The example in this topic is based on the Adventure Works 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.
    contact.SalesOrderHeaders.Add(newSalesOrder1);

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

    // Get all related ends
    IEnumerable<IRelatedEnd> relEnds =
        ((IEntityWithRelationships)contact).RelationshipManager
        .GetAllRelatedEnds();

    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);
    }
}

.NET Framework

Supported in: 4.5, 4, 3.5 SP1

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft