0 out of 1 rated this helpful - Rate this topic

ObjectStateManager Class

Maintains object state and identity management for entity type instances and relationship instances.

System.Object
  System.Data.Objects.ObjectStateManager

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

The ObjectStateManager type exposes the following members.

  Name Description
Public method ObjectStateManager Initializes a new instance of the ObjectStateManager class.
Top
  Name Description
Public property MetadataWorkspace Gets the MetadataWorkspace associated with this state manager.
Top
  Name Description
Public method ChangeObjectState Changes state of the ObjectStateEntry for a specific object to the specified entityState.
Public method ChangeRelationshipState(Object, Object, String, EntityState) Changes the state of the relationship between two entity objects that is specified based on the two related objects and the name of the navigation property.
Public method ChangeRelationshipState(Object, Object, String, String, EntityState) Changes the state of the relationship between two entity objects that is specified based on the two related objects and the properties of the relationship.
Public method ChangeRelationshipState<TEntity>(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.
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 GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetObjectStateEntries Returns a collection of ObjectStateEntry objects for objects or relationships with the given state.
Public method GetObjectStateEntry(EntityKey) Returns an ObjectStateEntry for the object or relationship entry with the specified key.
Public method GetObjectStateEntry(Object) Returns an ObjectStateEntry for the specified object.
Public method GetRelationshipManager Returns the RelationshipManager that is used by the specified object.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method TryGetObjectStateEntry(EntityKey, ObjectStateEntry) Tries to retrieve the corresponding ObjectStateEntry for the object or relationship with the specified EntityKey.
Public method TryGetObjectStateEntry(Object, ObjectStateEntry) Tries to retrieve the corresponding ObjectStateEntry for the specified Object.
Public method TryGetRelationshipManager Returns the RelationshipManager that is used by the specified object.
Top
  Name Description
Public event ObjectStateManagerChanged Occurs when entities are added to or removed from the state manager.
Top

ObjectStateManager tracks query results, and provides logic to merge multiple overlapping query results. It also performs in-memory change tracking when a user inserts, deletes, or modifies objects, and provides the change set for updates. This change set is used by the change processor to persist modifications.

This class is typically used by ObjectContext and not directly in applications.

These examples are based on the Adventure Works Sales Model.

The following example gets the ObjectStateManager from the ObjectContext and uses the state manager to access an object in the context.


int orderId = 43680;

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    ObjectStateManager objectStateManager = context.ObjectStateManager;
    ObjectStateEntry stateEntry = null;

    var order = (from o in context.SalesOrderHeaders
                 where o.SalesOrderID == orderId
                 select o).First();

    // Attempts to retrieve ObjectStateEntry for the given EntityKey.
    bool isPresent = objectStateManager.TryGetObjectStateEntry(((IEntityWithKey)order).EntityKey, out stateEntry);
    if (isPresent)
    {
        Console.WriteLine("The entity was found");
    }
}


The following example uses the TryGetObjectStateEntry(EntityKey, ObjectStateEntry) method on the returned ObjectStateManager to get an object based on its entity key.


private static void ApplyItemUpdates(SalesOrderDetail originalItem,
    SalesOrderDetail updatedItem)
{
    using (AdventureWorksEntities context =
        new AdventureWorksEntities())
    {
        context.SalesOrderDetails.Attach(updatedItem);
        // Check if the ID is 0, if it is the item is new. 
        // In this case we need to chage the state to Added.
        if (updatedItem.SalesOrderDetailID == 0)
        {
            // Because the ID is generated by the database we do not need to
            // set updatedItem.SalesOrderDetailID.
            context.ObjectStateManager.ChangeObjectState(updatedItem, System.Data.EntityState.Added);
        }
        else
        {
            // If the SalesOrderDetailID is not 0, then the item is not new
            // and needs to be updated. Because we already added the 
            // updated object to the context we need to apply the original values.
            // If we attached originalItem to the context 
            // we would need to apply the current values:
            // context.ApplyCurrentValues("SalesOrderDetails", updatedItem);
            // Applying current or original values, changes the state 
            // of the attached object to Modified.
            context.ApplyOriginalValues("SalesOrderDetails", originalItem);
        }
        context.SaveChanges();
    }
}


.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
Link to "Identity Resolution, State Management, and Change Tracking" article is wrong
The link to "Identity Resolution, State Management, and Change Tracking"  article is wrong. The correct one is:
http://msdn.microsoft.com/en-us/library/bb896269.aspx