ObjectStateEntry class
Using ObjectStateEntry class, we can monitor the entities whether they are Entity Framework generated objects or POCO objects.
When are ObjectStateEntry objects created?
We know that whenever we query in Entity Framework, the results are entity objects and are attached to the ObjectContext. For every attached entity in the ObjectContext, ObjectStateManager creates an ObjectStateEntry object. This object contains data and state of the entity. This means that through ObjectStateEntry objects, we can only manage attached objects residing in the context. Whenever we detach an entity from the Objectcontext, the ObjectStateEntry object will be deleted automatically.
How to access ObjectStateEntry object?
Each and every ObjectContext maintains its own ObjectStateManager and can be accessed by the ObjectStateManager property of ObjectContext using the statement given below.
After this statement, we can access ObjectStateEntry objects using the following two methods.
GetObjectStateEntries() method returns a collection of ObjectStateEntry objects from the context.
GetObjectStateEntry() method returns a single ObjectStateEntry object from the context.
The basic difference between these methods in usage is explained below.
The GetObjectStateEntry() method takes an entity as an input and usage of this method is shown below.
The GetObjectStateEntries() method takes the EntityState enums as a parameter and returns entites which are in the entity state which you have asked for. The possible EntityState enums are :
The below code demonstrates how to use GetObjectStateEntries() method to get all the ObjectStateEntry objects of customer entity which are modified. After this we can access the data of each entity´s current values and original values of each and every property.
The ObjectStateEntry class exposes properties to read and write the data and methods to change the state of an entity. Sometimes, it is necessary to change the state of an entity before calling SaveChanges method.
ObjectStateEntry class properties and methods are given below.
Properties
When are ObjectStateEntry objects created?
We know that whenever we query in Entity Framework, the results are entity objects and are attached to the ObjectContext. For every attached entity in the ObjectContext, ObjectStateManager creates an ObjectStateEntry object. This object contains data and state of the entity. This means that through ObjectStateEntry objects, we can only manage attached objects residing in the context. Whenever we detach an entity from the Objectcontext, the ObjectStateEntry object will be deleted automatically.
How to access ObjectStateEntry object?
Each and every ObjectContext maintains its own ObjectStateManager and can be accessed by the ObjectStateManager property of ObjectContext using the statement given below.
After this statement, we can access ObjectStateEntry objects using the following two methods.
- GetObjectStateEntries
- GetObjectStateEntry
GetObjectStateEntries() method returns a collection of ObjectStateEntry objects from the context.
GetObjectStateEntry() method returns a single ObjectStateEntry object from the context.
The basic difference between these methods in usage is explained below.
The GetObjectStateEntry() method takes an entity as an input and usage of this method is shown below.
The GetObjectStateEntries() method takes the EntityState enums as a parameter and returns entites which are in the entity state which you have asked for. The possible EntityState enums are :
- EntityState.Added
- EntityState.Modified
- EntityState.Unchanged
- EntityState.Deleted
The below code demonstrates how to use GetObjectStateEntries() method to get all the ObjectStateEntry objects of customer entity which are modified. After this we can access the data of each entity´s current values and original values of each and every property.
The ObjectStateEntry class exposes properties to read and write the data and methods to change the state of an entity. Sometimes, it is necessary to change the state of an entity before calling SaveChanges method.
ObjectStateEntry class properties and methods are given below.
Properties
- EntityKey
- Entity
- EntitySet
- State
- OriginalValues
- CurrentValues
- IsRelationship
- ObjectStateManager
- ApplyCurrentValues
- ApplyOriginalValues
- ChangeState
- DetectChanges
- AcceptChanges
- Delete
- SetModified
- SetModifiedProperty
- GetModifiedProperties
More Resources
- POCO class in Entity Framework
- POCO vs Entity Objects with comparison table
- Rules to be followed while creating POCOs - 4 important rules
- How to create a POCO class, integrate with ObjectContext, executing a query with POCOs
- How to load related POCO entities - loading patterns and their differences in their usage with examples
- How to perform Lazy Loading with POCO classes
- What is Change tracking in POCOs, ways of tracking
- Snapshot change tracking with DetectChanges() method
- Change tracking with proxies, How to create proxies?
- Instantiate POCO classes using ObjectContext.CreateObject method
- Using the DetectChanges() method to Fix-up Relationship in poco entities
- Entity Framework EntityKey object
- Retrieving a single entity with GetObjectByKey method of ObjectContext using EntityKey
- How to create an EntityKey in Entity Framework
- Loading Related entities in EF
- How to change the state of an entity using ChangeObjectState method of ObjectStateManager
- ChangeState method of ObjectStateEntry class
- Update an entity in a disconnected scenario using ApplyCurrentValues method
- Using ApplyOriginalValues method
- Entity state in Entity Framework - what, how, why?
- AcceptAllChanges and SaveChanges methods in Entity Framework
Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls
VB .Net
Constructors in Visual Basic.Net