The Entity state represents the state of an entity, and an entity is always in any one of the following states.
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.
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.
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.
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.
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.
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.
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.
|