This topic has not yet been rated - Rate this topic

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.

  Name Description
Public method Static member Create Creates a new RelationshipManager object.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetAllRelatedEnds Returns an enumeration of all the related ends managed by the relationship manager.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetRelatedCollection<TTargetEntity> Gets an EntityCollection<TEntity> of related objects with the specified relationship name and target role name.
Public method GetRelatedEnd Returns either an EntityCollection<TEntity> or EntityReference<TEntity> of the correct type for the specified target role in a relationship.
Public method GetRelatedReference<TTargetEntity> Gets the EntityReference<TEntity> for a related object by using the specified combination of relationship name and target role name.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method InitializeRelatedCollection<TTargetEntity> Initializes an existing EntityCollection<TEntity> that was created by using the default constructor.
Public method InitializeRelatedReference<TTargetEntity> Initializes an existing EntityReference<TEntity> that was created by using the default constructor.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method OnDeserialized Infrastructure. Used internally to deserialize entity objects along with the RelationshipManager instances.
Public method OnSerializing Called by Object Services to prepare an EntityKey for binary serialization with a serialized relationship.
Public method ToString Returns 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, 3.5 SP1

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ