In this article, I will discuss the Entity States in Entity Framework. Please read our previous article where we discussed Entities 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 hasnt been modified
- Detached: The entity isnt 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 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 thats automatically handled by the context class. All other changes must be made explicitly by using the proper methods of DbContext class. The following diagram shows the different states of an entity in the Entity Framework.
Lets discuss different states.
Unchanged State
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.
Detached State
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.
Added State
Whenever we add a new entity to the context using the 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
Modified State:
The entity will be in a 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 are set for only those columns, whose values are modified.
Deleted State
Whenever we call the 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 Videos.