This documentation is archived and is not being maintained.

ObjectStateManager.ChangeRelationshipState<TEntity> Method (TEntity, Object, Expression<Func<TEntity, Object>>, EntityState)

Changes the state of the relationship between two entity objects that is specified based on the two related objects and a LINQ expression that defines the navigation property.

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

public ObjectStateEntry ChangeRelationshipState<TEntity>(
	TEntity sourceEntity,
	Object targetEntity,
	Expression<Func<TEntity, Object>> navigationPropertySelector,
	EntityState relationshipState
where TEntity : class

Type Parameters


The entity type of the source object.


Type: TEntity
The object instance or EntityKey of the source entity at one end of the relationship.
Type: System.Object
The object instance or EntityKey of the target entity at the other end of the relationship.
Type: System.Linq.Expressions.Expression<Func<TEntity, Object>>
A LINQ expression that selects the navigation property on source that returns the specified target.
Type: System.Data.EntityState
The requested EntityState of the specified relationship.

Return Value

Type: System.Data.Objects.ObjectStateEntry
The ObjectStateEntry for the relationship that was changed.


When source, target, or selector is null.


selector is malformed or cannot return a navigation property.


When you try to change the state of the relationship to a state other than Deleted or Detached when either source or target is in a Deleted state.


When you try to change the state of the relationship to a state other than Added or Detached when either source or target is in an Added state.


When state is not a valid EntityState value.

Entries for the objects at both ends of the specified relationship must exist in the ObjectStateManager.


This method is not supported for relationships that are based on a foreign key association.

.NET Framework

Supported in: 4

.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.