Entity state in Entity Framework
This article explains
1. What is an entity state in Entity Framework?
2. The different states of an entity and how to change the state of an entity and how to retrieve an entity state?
Entity state represents the state of an entity. An entity is always in any one of the following states.
Entity state can be retrieved by using ObjectStateEntry class´s State property. With the code example given below, we first access the ObjectStateEntry object of a particular customer entity using GetObjectStateEntry() method. After that we can use State property to get the state of an entity.
In the code example, the entity state is displayed as Unchanged because the entities are just returned from the database and no changes have been done. How is entity state important in persisting changes to the database? Persistence process depends on the entity state and happens only for entities which are in any one of the following states - Added, Deleted and Modified. Persistence does not happen for entities which are in the Unchanged and Detached.
Entities which are in Added state are persisted as new rows to the database by the INSERT command and entities in a Modified state are persisted by UPDATE command and entities in a Deleted state are deleted by DELETE command from the database.
Let me explain what each state represents briefly.
Added state
Whenever we add a new entity to the context using AddObject() method, the state of the entity will be in Added state.
Unchanged
This is the default state the entities will be in when we perform query and also whenever we attach an entity to the context using Attach() method.
Deleted state
Whenever we call DeleteObject() method, the entity will be deleted from the context and will be marked as "Deleted". When SaveChanges method is called, the corresponding rows are deleted from the database.
Modified state
The entity will be in Modified state whenever we modify scalar properties.
Detached state
Whenever we use Detach() method, the entity will be in Detached state. Once the entity is in Detached state, it cannot be tracked by the ObjectContext. We have to use Attach() method for the entity to be tracked by the ObjectContext.
Why change an entity state?
In a web application, even though the client (web form) has modified an entity and if we attach such an entity to the context using Attach() method, the entity will be marked as Unchanged. Now, if we persist this entity, the changes are not reflected in the database. So, before calling SaveChanges() method, we need to change the state of an entity from Unchanged to Modified explicitly. Only then, changes are persisted by UPDATE command.
How to change an entity state?
We change an entity state using any of the following methods and each method works differently and each one has its own pros and cons.
If you are using EntityObjects in an application, whenever there is a change in scalar property, the object notifies the change so that context (but actually the ObjectStateManager) is able to track the state of the entity. But if you are using POCO classes, we need to use snapshot change tracking mechanism using DetectChanges method or use change tracking proxies to track changes in the entities.
1. What is an entity state in Entity Framework?
2. The different states of an entity and how to change the state of an entity and how to retrieve an entity state?
Entity state represents the state of an entity. An entity is always in any one of the following states.
- Added
- Deleted
- Modified
- Unchanged
- Detached
Entity state can be retrieved by using ObjectStateEntry class´s State property. With the code example given below, we first access the ObjectStateEntry object of a particular customer entity using GetObjectStateEntry() method. After that we can use State property to get the state of an entity.
In the code example, the entity state is displayed as Unchanged because the entities are just returned from the database and no changes have been done. How is entity state important in persisting changes to the database? Persistence process depends on the entity state and happens only for entities which are in any one of the following states - Added, Deleted and Modified. Persistence does not happen for entities which are in the Unchanged and Detached.
Entities which are in Added state are persisted as new rows to the database by the INSERT command and entities in a Modified state are persisted by UPDATE command and entities in a Deleted state are deleted by DELETE command from the database.
Let me explain what each state represents briefly.
Added state
Whenever we add a new entity to the context using AddObject() method, the state of the entity will be in Added state.
Unchanged
This is the default state the entities will be in when we perform query and also whenever we attach an entity to the context using Attach() method.
Deleted state
Whenever we call DeleteObject() method, the entity will be deleted from the context and will be marked as "Deleted". When SaveChanges method is called, the corresponding rows are deleted from the database.
Modified state
The entity will be in Modified state whenever we modify scalar properties.
Detached state
Whenever we use Detach() method, the entity will be in Detached state. Once the entity is in Detached state, it cannot be tracked by the ObjectContext. We have to use Attach() method for the entity to be tracked by the ObjectContext.
Why change an entity state?
In a web application, even though the client (web form) has modified an entity and if we attach such an entity to the context using Attach() method, the entity will be marked as Unchanged. Now, if we persist this entity, the changes are not reflected in the database. So, before calling SaveChanges() method, we need to change the state of an entity from Unchanged to Modified explicitly. Only then, changes are persisted by UPDATE command.
How to change an entity state?
We change an entity state using any of the following methods and each method works differently and each one has its own pros and cons.
If you are using EntityObjects in an application, whenever there is a change in scalar property, the object notifies the change so that context (but actually the ObjectStateManager) is able to track the state of the entity. But if you are using POCO classes, we need to use snapshot change tracking mechanism using DetectChanges method or use change tracking proxies to track changes in the entities.
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
- ObjectStateEntry class
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