Calling SaveChanges when database objects have not been changed c# dbcontext entity-framework-6 savechanges


I am coding a MVC 5 internet application with EF6 and I have a question about what happens when the context.SaveChanges() method is called.

Does the DbContext object store the CRUD actions that occur, such that if the SaveChanges() method is called, and none of the data in the database is updated, no action occurs?

Here is an example:

  1. I create a simple model object, then add this to the database and call SaveChanges.
  2. I retrieve this simple object, set one of the objects values to be the same as it currently is, and then call SaveChanges.

In the above example, are any changes made to the database? Do any database transactions occur?

I have done some research, but could not find the answer to this question.

Thanks in advance.

5/18/2015 4:58:33 AM

Accepted Answer

EF6 uses the change tracker to detect if there are any changes.
When you do a call to SaveChanges(), one of the first things EF6 is doing is calling DetectChanges() which will inspect the change tracker.

Now suppose you really want to know if something has changed you can look in the changetracker yourself too. This post shows how in the answer :
Entity Framework 6: audit/track changes

5/23/2017 11:45:44 AM

Popular Answer

No, if entitys are not changed, it's state is unchanged, so EF is not interested in those.

You can force saving by changing state of entity manualy if required.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow