Entity State in Entity Framework
In this article, I will discuss the Entity State in Entity Framework. The Entity state represents the state of an entity. An entity is always in any one of the following states.
- Added: The entity is marked as added.
- Deleted: The entity is marked as deleted.
- Modified: The entity has been modified.
- Unchanged: The entity hasn’t been modified
- Detached: The entity isn’t tracked.
EF maintains the state of each entity during its lifetime. Each entity has a state based on the operation performed on it via the context class (the class which is derived from DbContext class). The entity state represented by an enum i.e. System.Data.Entity.EntityState which has the following values.
The Context not only holds the reference to all the entity objects as soon as retrieved from the database but also keeps track of entity states and maintains modifications made to the properties of the entity. This feature is known as Change Tracking.
State Changes in the Entity Lifecycle
The change in the entity state from the Unchanged to the Modified state is the only state that’s automatically handled by the context class. All other changes must be made explicitly by using proper methods of DbContext class.
The following Diagram shows the different state of an entity in Entity Framework.
Let’s discuss different states.
The property values of the entity have not been modified since it was retrieved from the database. SaveChanges ignores this entity.
This is the default state the entities will be in when we perform the query and also whenever we attach an entity to the context using Attach() method.
Whenever we use Detach() method, the entity will be in the Detached state. Once the entity is in the Detached state, it cannot be tracked by the ObjectContext. We have to use Attach() method for the entity to be tracked by the ObjectContext.
The Detached entity state indicates that the entity is not being tracked by the context.
Whenever we add a new entity to the context using AddObject() method, the state of the entity will be in the Added state.
Added entity state indicates that the entity exists in the context, but does not exist in the database. DbContext generates the INSERT SQL query and insert the data into the database when the saveChanges method is invoked. Once the saveChanges are successful the state of the entity is changed to Unchanged
The entity will be in Modified state whenever we modify scalar properties.
The Modified entity state indicates that the entity is modified but not updated in the database. It also indicates that the entity exists in the database. The Dbcontext generates the update SQL Query to remove the entity from the database. Once the saveChanges is successful the state of the entity is changed to Unchanged
In the Connected environment, the Entity framework also keeps track of the properties that have been modified. The Columns in the Update statement is set for only those columns, whose values are modified.
Whenever we call DeleteObject() method, the entity will be deleted from the context and will be marked as “Deleted”. When the SaveChanges method is called, the corresponding rows are deleted from the database.
The Deleted entity state indicates that the entity is marked for deletion, but not yet deleted from the database. It also indicates that the entity exists in the database. The DbContext generates the delete SQL Query to remove the entity from the database. The entity is removed from the context once the delete operation succeeds after the saveChanges
Thus, entity states play an important role in Entity Framework. We will discuss all these entity states with the example in our upcoming articles.
In the next article, I will discuss in which environment which approach we need to follow.
In this article, I try to explain the Entity States in Entity Framework. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article