Monday, November 18, 2019

Entity Framework Core - Add, Remove, Attach and Update


In this post I will explain how the Add / Remove / Attach and Update affects the EntityState and how Entiy Framework behaves based on each state.

Before we start talking about the differences of each of the methods above is important to understand the five different states defined by Entity Framework.

Entity states

These states are defined in the EntityState Enum, see below the description of each one:

  • Added: the entity is being tracked by the context but does not yet exist in the database
  • Unchanged: the entity is being tracked by the context and exists in the database, and its property values have not changed from the values in the database
  • Modified: the entity is being tracked by the context and exists in the database, and some or all of its property values have been modified
  • Deleted: the entity is being tracked by the context and exists in the database, but has been marked for deletion from the database the next time SaveChanges is called
  • Detached: the entity is not being tracked by the context


SaveChanges

The SaveChanges method will work in different ways based on the EntityState.
See below the behavior for each EntityState:

  • Unchanged entities are not touched by SaveChanges. Updates are not sent to the database for entities in the Unchanged state.
  • Added entities are inserted into the database and then become Unchanged when SaveChanges returns.
  • Modified entities are updated in the database and then become Unchanged when SaveChanges returns.
  • Deleted entities are deleted from the database and are then detached from the context.


Add, Remove, Attach and Update

Now that we now the Entity Framework EntityStates and how the SaveChanges work with each state will be easy to understand how the methods Add / Remove / Attach and Update works.

Add

The Add method will add the object to the context, which means that the object will start being tracked by Entity Framework and its Entity State will be EntityState.Added.
Once SaveChanges is called a new record will be added to the database.

See below the Entity State in the Immediate Window:


Attach

The Attach method will add the object to the context, which means that the object will start being tracked by Entity Framework and its Entity State will be EntityState.Unchanged.
Once SaveChanges is called  Entity framework won`t touch this object.


See below the Entity State in the Immediate Window:


Remove

The Remove method will add the object to the context, which means that the object will start being tracked by Entity Framework and its Entity State will be EntityState.Removed.
Once SaveChanges is called  Entity framework will delete the record from the database.


See below the Entity State in the Immediate Window:

Update

The Update method will add the object to the context, which means that the object will start being tracked by Entity Framework and its Entity State will be EntityState.Modified.
Once SaveChanges is called, Entity framework will update the record in the database.


See below the Entity State in the Immediate Window:


Conclusion

As we can see all the four methods above will tell Entity Framework to start tracking the objects, but each of them will set different States to the entity and then Entity framework will execute a different operation in the database when SaveChanges is called.

1 comment:

  1. I found Hubwit as a transparent s ite, a social hub which is a conglomerate of Buyers and Sellers who are ready to offer online digital consultancy at decent cost. Kernbohrungen

    ReplyDelete

Microservices – Creating Resilient Services

Dealing with unexpected failures is one of the hardest problems to solve, especially in a distributed system . A microservice needs to...