Update Record in Entity Framework

Learn how an entity framework update records to the database. We can update records either in connected or disconnected scenarios. In the connected Scenario, we open the context, query for the entity, edit it, and call the SaveChanges method. In the Disconnected scenario, we already have the entity with use. Hence all we need to is to attach/add it to the context. Set its State as Modified and then call the SaveChanges But we need to be careful in a disconnected scenario as the update will update all the fields, Hence a chance of accidentally overwriting a field. We also show you how to update multiple records & Related Data etc.

Source Code:
The source code of this project available in GitHub.

Update the Records

Updating the entity involves getting the entity from the database, make the necessary changes, and then call the SaveChanges to persist the changes in the database.

There are two Scenario’s that arise, when you update the data to the database.

  1. Connected scenario
  2. Disconnected scenario

Let us look at both scenarios.

Update Records in Connected Scenario

The responsibility of updating the records falls with the DBContext class. Whenever we make a query to the database, the context retrieves it and mark the entity as Unchanged. It then starts to track any changes made to it a process we call it as Change Tracking. Whenever we make changes to the entity, the context marks the entity as Modified. If we add a new entity to the context using the Add or AddRange it will mark it as Added. If you use the Remove to delete the entity, it marks it as Deleted. When we call the SaveChanges the context creates an insert, update or delete SQL command depending on the state of the entity to update the database.

The above scenario works correctly if the entity is connected with context, which loads it. i.e the context is not closed or disposed of. We call this connected scenario.

Best Entity Framework Books
The Best Entity Freamework Books, which helps you to get started with Entity Framework  


The following example updates the Descr field of Accounts Department in Connected Scenario.

First, we create a new context and retrieve the existing department data from the database. We modify the Descr field. Since the context is open, it will mark the entity as Modifiedtransparently. Finally, when we call the SaveChanges, the Context generates the update SQL statement to persist the change to the database.

If you log the SQL Command, you will see the following Update Statement. Note that query only updates the Descr field and not the other fields. This is because, context not only tracks the entity as a whole, but also the individual properties.

Update Records in Disconnected Scenario

The connected scenario is not always possible in real life apps.

For Example, in a web application, the user requests for the Department model. We create a new instance of the context, fetch the data, close the context, and sends the data back to the user. When the user asks to update the Department, we will create a new instance of the context. The newly created context is not aware of the Department model. Hence it won’t update the database if you call SaveChanges. To update this entity we need to attach the Department to the context and inform it to mark its status as Modified. Now if we call the SaveChanges method, the context will send an update query to the database.


In the following example, we load the Purchase department and close the context. Now we make the changes to the Descr field.

Now, we open a new context. This new context is not aware of any Department model. Hence we need it to attach it to the context and set its state as Modified. We do that using the Entry method of the Context. The Entry method allows us to view the tracked entities, modify their status, etc. We set the State of the entity to Modified.

Finally, we invoke the SaveChanges method to update the database.

If you take a look at the SQL update query, you will see the difference between the query created in connected & disconnected scenario. In connected Scenario, the query updates only the Descr filed. i.e because context is tracking it and knows which fields is changed. While in disconnected Scenario, the update query includes all the fields, because context does not know which fields are modified.

We can also use the Attach or the Add method to add the entity to the context. But remember that the Add method adds the entity with the state as Added. While if you use the Attach method, the entity is added with the state as Unchanged

Updating Records

You need to careful when updating the records in a disconnected way. For Example consider the case, where you need to update the Department record and change the Descr field.

The Wrong Way

In this example, updateDepartment1 gets id of the Department & Descr to update as the argument.

We create a new Department and assign the DepartmentID & Descr. We attach this entity to context and set its state as Modified. The SaveChanges will update the record with DepartmentID is 2 with the new Descr Value. But it will also update the Name field as Null as we have not provided any value to it.

The Right Way

The correct way it to query and load the Department entity. Update only changed fields and invoke SaveChanges. In this way you will not overwrite any fields accidentally and the context generates the update query efficiently.

Update Multiple Records

In case of multiple Records, you can loop through them, query the department from the database, update the changed the field and call SaveChanges to update the database.


3 thoughts on “Update Record in Entity Framework”

  1. Tutorial is good but no proper indexing. There should be side bar or something to check what all points are there in the tutorial.
    It’s like maze once you enter tutorial

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top