ObjectStateManager Class
Maintains object state and identity management for entity type instances and relationship instances.
Assembly: System.Data.Entity (in System.Data.Entity.dll)
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.
Dim orderId As Integer = 43680 Using advWorksContext As New AdventureWorksEntities Try Dim objectStateManager As ObjectStateManager = _ advWorksContext.ObjectStateManager Dim stateEntry As ObjectStateEntry = Nothing Dim order As SalesOrderHeader = _ advWorksContext.SalesOrderHeader _ .Where("it.SalesOrderID = @id", _ New ObjectParameter("id", orderId)).First ' Attempts to retrieve ObjectStateEntry for the given EntityKey. If objectStateManager.TryGetObjectStateEntry(CType(order, IEntityWithKey) _ .EntityKey, stateEntry) Then Console.WriteLine("The entity was found") End If Catch ex As EntitySqlException Console.WriteLine(ex.ToString) Catch ex As EntityCommandExecutionException Console.WriteLine(ex.ToString) End Try 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 Shared Sub ApplyItemUpdates(ByVal originalItem As SalesOrderDetail, _ ByVal updatedItem As SalesOrderDetail) Using advWorksContext As AdventureWorksEntities = _ 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 ex As InvalidOperationException Console.WriteLine(ex.ToString()) End Try End Using End Sub
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.