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.
How to retrieve an entity state?
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.
Whenever we add a new entity to the context using AddObject() method, the state of the entity will be in Added state.
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.
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.
The entity will be in Modified state whenever we modify scalar properties.
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.