Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

ObjectStateManager Class

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

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

public class ObjectStateManager

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 advWorksContext =
    new AdventureWorksEntities())
{
    try
    {
        ObjectStateManager objectStateManager = advWorksContext.ObjectStateManager;
        ObjectStateEntry stateEntry = null;

        SalesOrderHeader order =
            advWorksContext.SalesOrderHeader.Where
            ("it.SalesOrderID = @id", new ObjectParameter(
             "id", orderId)).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");
        }
    }
    catch (EntitySqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
    catch (EntityCommandExecutionException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

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 advWorksContext = 
        new AdventureWorksEntities())
    {
        try
        {
            // Attach the original item to the object context.
            advWorksContext.Attach(originalItem);

            // Call the ApplyPropertyChanges method to apply changes 
            // from the updated item to the original version.
            advWorksContext.ApplyPropertyChanges("SalesOrderDetail",
                updatedItem);

            advWorksContext.SaveChanges();
        }
        catch (InvalidOperationException ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}

System.Object
  System.Data.Objects.ObjectStateManager

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

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:
© 2015 Microsoft