Managing Entity State


Managing Entity State

The Entity state represents the state of an entity, and an entity is always in any one of the following states.

  1. Added: The entity is marked as added.
  2. Deleted: The entity is marked as deleted.
  3. Modified: The entity has been changed.
  4. Unchanged: The entity hasn’t been modified
  5. Detached: The entity isn’t tracked.

EF maintains the state of each entity during its lifetime. Each entity has a state based on its operation via the context class (the class derived from DbContext class). The entity state is represented by an enum, i.e. System.Data.Entity.EntityState

The Context holds the reference to all the entity objects as soon as retrieved from the database and keeps track of entity states and maintains modifications made to the entity's properties. 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 the proper methods of DbContext class. The following diagram shows the different states in the Entity Framework.

Unchanged State

The entity's property values 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 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 to track the entity by the ObjectContext. The Detached entity state indicates that the Context is not tracking the entity.

Added State

Whenever we add a new entity to the Context using the AddObject() method, the entity's state 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 inserts the data into the database when invoked the saveChanges method. 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 limited but not updated in the database. It also shows 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 reached, 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, and it also shows 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.