ObjectStateManager ObjectStateManager ObjectStateManager ObjectStateManager Class

Definition

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

public ref class ObjectStateManager
public class ObjectStateManager
type ObjectStateManager = class
Public Class ObjectStateManager
Inheritance
ObjectStateManagerObjectStateManagerObjectStateManagerObjectStateManager

Examples

These examples are based on the AdventureWorks 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");
    }
}
Dim orderId As Integer = 43680

Using context As New AdventureWorksEntities()
    Dim objectStateManager As ObjectStateManager = context.ObjectStateManager
    Dim stateEntry As ObjectStateEntry = Nothing

    Dim order = (From o In context.SalesOrderHeaders
        Where o.SalesOrderID = orderId
        Select o).First()

    ' Attempts to retrieve ObjectStateEntry for the given EntityKey. 
    Dim isPresent As Boolean = objectStateManager.TryGetObjectStateEntry(DirectCast(order, IEntityWithKey).EntityKey, stateEntry)
    If isPresent Then
        Console.WriteLine("The entity was found")
    End If
End Using

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();
    }
}
Private Shared Sub ApplyItemUpdates(ByVal originalItem As SalesOrderDetail, ByVal updatedItem As SalesOrderDetail)
    Using context As 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 Then
            ' 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)
        End If
        context.SaveChanges()
    End Using
End Sub

Remarks

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.

Constructors

ObjectStateManager(MetadataWorkspace) ObjectStateManager(MetadataWorkspace) ObjectStateManager(MetadataWorkspace) ObjectStateManager(MetadataWorkspace)

Initializes a new instance of the ObjectStateManager class.

Properties

MetadataWorkspace MetadataWorkspace MetadataWorkspace MetadataWorkspace

Gets the MetadataWorkspace associated with this state manager.

Methods

ChangeObjectState(Object, EntityState) ChangeObjectState(Object, EntityState) ChangeObjectState(Object, EntityState) ChangeObjectState(Object, EntityState)

Changes state of the ObjectStateEntry for a specific object to the specified entityState.

ChangeRelationshipState(Object, Object, String, EntityState) ChangeRelationshipState(Object, Object, String, EntityState) ChangeRelationshipState(Object, Object, String, EntityState) 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.

ChangeRelationshipState(Object, Object, String, String, EntityState) ChangeRelationshipState(Object, Object, String, String, EntityState) ChangeRelationshipState(Object, Object, String, String, EntityState) 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.

ChangeRelationshipState<TEntity>(TEntity, Object, Expression<Func<TEntity,Object>>, EntityState) ChangeRelationshipState<TEntity>(TEntity, Object, Expression<Func<TEntity,Object>>, EntityState) ChangeRelationshipState<TEntity>(TEntity, Object, Expression<Func<TEntity,Object>>, EntityState) 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.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetObjectStateEntries(EntityState) GetObjectStateEntries(EntityState) GetObjectStateEntries(EntityState) GetObjectStateEntries(EntityState)

Returns a collection of ObjectStateEntry objects for objects or relationships with the given state.

GetObjectStateEntry(EntityKey) GetObjectStateEntry(EntityKey) GetObjectStateEntry(EntityKey) GetObjectStateEntry(EntityKey)

Returns an ObjectStateEntry for the object or relationship entry with the specified key.

GetObjectStateEntry(Object) GetObjectStateEntry(Object) GetObjectStateEntry(Object) GetObjectStateEntry(Object)

Returns an ObjectStateEntry for the specified object.

GetRelationshipManager(Object) GetRelationshipManager(Object) GetRelationshipManager(Object) GetRelationshipManager(Object)

Returns the RelationshipManager that is used by the specified object.

GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)
TryGetObjectStateEntry(EntityKey, ObjectStateEntry) TryGetObjectStateEntry(EntityKey, ObjectStateEntry) TryGetObjectStateEntry(EntityKey, ObjectStateEntry) TryGetObjectStateEntry(EntityKey, ObjectStateEntry)

Tries to retrieve the corresponding ObjectStateEntry for the object or relationship with the specified EntityKey.

TryGetObjectStateEntry(Object, ObjectStateEntry) TryGetObjectStateEntry(Object, ObjectStateEntry) TryGetObjectStateEntry(Object, ObjectStateEntry) TryGetObjectStateEntry(Object, ObjectStateEntry)

Tries to retrieve the corresponding ObjectStateEntry for the specified Object.

TryGetRelationshipManager(Object, RelationshipManager) TryGetRelationshipManager(Object, RelationshipManager) TryGetRelationshipManager(Object, RelationshipManager) TryGetRelationshipManager(Object, RelationshipManager)

Returns the RelationshipManager that is used by the specified object.

Events

ObjectStateManagerChanged ObjectStateManagerChanged ObjectStateManagerChanged ObjectStateManagerChanged

Occurs when entities are added to or removed from the state manager.

Applies to