This topic has not yet been rated - Rate this topic

ObjectStateEntry Class

Maintains state and key information for objects and relationships and change tracking for object properties.

System.Object
  System.Data.Objects.ObjectStateEntry

Namespace:  System.Data.Objects
Assembly:  System.Data.Entity (in System.Data.Entity.dll)
public abstract class ObjectStateEntry : IEntityChangeTracker

The ObjectStateEntry type exposes the following members.

  Name Description
Public property CurrentValues Gets the current property values of the object or relationship associated with this ObjectStateEntry.
Public property Entity Gets the object associated with this ObjectStateEntry.
Public property EntityKey Gets the EntityKey associated with the ObjectStateEntry.
Public property EntitySet Gets the EntitySetBase for the object or relationship associated with this ObjectStateEntry.
Public property IsRelationship Gets a Boolean value that indicates whether this ObjectStateEntry represents a relationship.
Public property ObjectStateManager Gets the ObjectStateManager for this ObjectStateEntry.
Public property OriginalValues Gets the read-only version of original values of the object or relationship associated with this ObjectStateEntry. To get updatable original values, use GetUpdatableOriginalValues.
Public property RelationshipManager Returns a RelationshipManager instance for the object represented by entry.
Public property State Gets the state of this ObjectStateEntry.
Top
  Name Description
Public method AcceptChanges Accepts the current values as original values.
Public method ApplyCurrentValues Sets the current values of the entry to match the property values of a supplied object.
Public method ApplyOriginalValues Sets the original values of the entry to match the property values of a supplied object.
Public method ChangeState Changes state of the entry to the specified EntityState value.
Public method Delete Marks an entity as deleted.
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 GetModifiedProperties Returns the names of an object's properties that have changed since the last time SaveChanges was called.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method GetUpdatableOriginalValues Gets the updatable version of original values of the object associated with this ObjectStateEntry.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method SetModified Sets the state of the object or relationship to Modified.
Public method SetModifiedProperty Marks the specified property as modified.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top
  Name Description
Explicit interface implemetation Private method IEntityChangeTracker.EntityComplexMemberChanged Notifies the state manager that a complex property has changed.
Explicit interface implemetation Private method IEntityChangeTracker.EntityComplexMemberChanging Notifies the state manager that a complex property has a pending change.
Explicit interface implemetation Private method IEntityChangeTracker.EntityMemberChanged Notifies the state manager that a property has changed.
Explicit interface implemetation Private method IEntityChangeTracker.EntityMemberChanging Notifies the state manager that a property has a pending change.
Explicit interface implemetation Private property IEntityChangeTracker.EntityState Gets the EntityState for the ObjectStateEntry.
Top

Maintains the EntityState, the EntityKey values, and the original values of an object or relationship. Also manages the list of modified properties.

There is an instance of an ObjectStateEntry associated with each entity type relationship instance. An object is associated with an ObjectStateEntry only if it is in the ObjectStateManager.

When an object that has a relationship is detached, the information maintained by the ObjectStateEntry is reduced to only what is required to maintain the relationship.

An ObjectStateEntry cannot have the same key as another ObjectStateEntry in the same ObjectStateManager.

The key values of a persisted entity cannot be modified. Entities in the unchanged, modified, and deleted states are persisted entities.

The example in this topic is based on the Adventure Works Sales Model. The example gets the ObjectStateEntry for the given EntityKey from the ObjectStateManager. Then it gets the current value of SalesOrderHeader.PurchaseOrderNumber property, changes the property's value, and enumerates through the collection of modified properties.


int orderId = 43680;

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

    // Get ObjectStateEntry from EntityKey.
    ObjectStateEntry stateEntry =
        context.ObjectStateManager
        .GetObjectStateEntry(((IEntityWithKey)order).EntityKey);

    //Get the current value of SalesOrderHeader.PurchaseOrderNumber.
    CurrentValueRecord rec1 = stateEntry.CurrentValues;
    string oldPurchaseOrderNumber =
        (string)rec1.GetValue(rec1.GetOrdinal("PurchaseOrderNumber"));

    //Change the value.
    order.PurchaseOrderNumber = "12345";
    string newPurchaseOrderNumber =
        (string)rec1.GetValue(rec1.GetOrdinal("PurchaseOrderNumber"));

    // Get the modified properties.
    IEnumerable<string> modifiedFields = stateEntry.GetModifiedProperties();
    foreach (string s in modifiedFields)
        Console.WriteLine("Modified field name: {0}\n Old Value: {1}\n New Value: {2}",
            s, oldPurchaseOrderNumber, newPurchaseOrderNumber);

    // Get the Entity that is associated with this ObjectStateEntry.
    SalesOrderHeader associatedEnity = (SalesOrderHeader)stateEntry.Entity;
    Console.WriteLine("Associated Enity's ID: {0}", associatedEnity.SalesOrderID);
}


.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
The example in VB does not work
  Console.WriteLine("Modified field name: {0}", s)
	Console.WriteLine("Old Value: {1}", oldPurchasedNumber)
	Console.WriteLine("New Value: {2}", newPurchasedNumber)

' should be

Console.WriteLine("Modified field name: {0}", s) Console.WriteLine("Old Value: {0}", oldPurchasedNumber) Console.WriteLine("New Value: {0}", newPurchasedNumber)