1

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.

Simon
  • 7,991
  • 21
  • 83
  • 163

2 Answers2

2

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

Community
  • 1
  • 1
Philip Stuyck
  • 7,344
  • 3
  • 28
  • 39
0

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.

madoxdev
  • 3,770
  • 1
  • 24
  • 39