This documentation is archived and is not being maintained.

ObjectStateEntry Class

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

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

'Declaration
Public MustInherit Class ObjectStateEntry _
	Implements IEntityChangeTracker
'Usage
Dim instance As ObjectStateEntry

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.

Dim orderId As Integer = 43680

Using advWorksContext As New AdventureWorksEntities
    Try 
        Dim order As SalesOrderHeader = _
            advWorksContext.SalesOrderHeader.Where("it.SalesOrderID = @id", _
                New ObjectParameter("id", orderId)).First()

        ' Get ObjectStateEntry from EntityKey. 
        Dim stateEntry As ObjectStateEntry = _
            advWorksContext.ObjectStateManager.GetObjectStateEntry(CType(order, IEntityWithKey).EntityKey)

        'Get the current value of SalesOrderHeader.PurchaseOrderNumber. 
        Dim rec1 As CurrentValueRecord = stateEntry.CurrentValues
        Dim oldPurchaseOrderNumber As String = _
            CStr(rec1.GetValue(rec1.GetOrdinal("PurchaseOrderNumber")))

        'Change the value.
        order.PurchaseOrderNumber = "12345" 
        Dim newPurchaseOrderNumber As String = _
            CStr(rec1.GetValue(rec1.GetOrdinal("PurchaseOrderNumber")))

        ' Get the modifed properties. 
        Dim modifiedFields As IEnumerable(Of String) = stateEntry.GetModifiedProperties

        ' Get the Entity that is associated with this ObjectStateEntry. 
        Dim s As String 
        For Each s In modifiedFields
            Console.WriteLine("Modified field name: {0}" & ChrW(10) & _
                              " Old Value: {1}" & ChrW(10) & _
                              " New Value: {2}", _
                              s, oldPurchaseOrderNumber, newPurchaseOrderNumber)
        Next 

        Dim associatedEnity As SalesOrderHeader = CType(stateEntry.Entity, SalesOrderHeader)

        Console.WriteLine("Associated Enity's ID: {0}", _
            associatedEnity.SalesOrderID)

    Catch ex As EntitySqlException
        Console.WriteLine(ex.ToString)
    Catch ex As EntityCommandExecutionException
        Console.WriteLine(ex.ToString)
    End Try 
End Using

System.Object
  System.Data.Objects.ObjectStateEntry

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
Show: