Export (0) Print
Expand All

IEntityWithRelationships.RelationshipManager Property

Returns the relationship manager that manages relationships for an instance of an entity type.

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

RelationshipManager RelationshipManager { get; }

Gets the RelationshipManager for an entity type instance. Object Services uses this property to provide access to related objects.

Classes that expose relationships must implement this property by constructing and setting the RelationshipManager in their constructor. The implementation of this property should use the static method Create to create a new RelationshipManager when needed. Once created, it is expected that this object will be stored on the entity and will be provided through this property. For more information, see Implementing an Entity Data Model with Custom Objects (Entity Framework).

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 through the relationship manager. Then it displays relationship name, source role name and target role name for each related end.

using (AdventureWorksEntities advWorksContext =
    new AdventureWorksEntities())
{
    Contact contact = new Contact();

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

    // Create another SalesOrderHeader.
    SalesOrderHeader newSalesOrder2 = new SalesOrderHeader();
    // Add SalesOrderHeader to the Contact.
    contact.SalesOrderHeader.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);
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

.NET Framework

Supported in: 3.5 SP1

Community Additions

ADD
Show:
© 2014 Microsoft