Loading

Microsoft Entity Framework

What is Entity States in Entity Framework?. The Complete Microsoft Entity Framework Developer Course 2023 [Videos].

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.

  1. Added: The entity is marked as added.
  2. Deleted: The entity is marked as deleted.
  3. Modified: The entity has been modified.
  4. Unchanged: The entity hasnt been modified
  5. 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.

Entity States in Entity Framework

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.

State Changes in the Entity Lifecycle

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.

See All

Comments (434 Comments)

Submit Your Comment

See All Posts

Related Posts

Microsoft Entity Framework / Audio Video

How we can install sql server and create db?

How we can install sql server and create db?
24-aug-2024 /37 /434

Microsoft Entity Framework / Blog

What is the Entity Framework?

Entity Framework is an Open-Source Object-Relational Mapping (ORM) Framework for .NET applications that enables .NET developers to work with relational data using domain-specific objects without focusing on the underlying database tables and columns where actually the data is stored. That means the Entity Framework eliminates the need for writing the data-access code that developers usually need to write.
11-Feb-2022 /37 /434

Microsoft Entity Framework / Blog

What is Entity Framework Architecture?

In this article, I am going to discuss the Entity Framework Architecture in Detail. Please read our previous article where we discussed the Overview of the Entity Framework. At the end of this article, you will understand the following pointers in detail.
11-Feb-2022 /37 /434