Search code examples
entity-framework-5savechanges

Are EF data changes ONLY persisted when SaveChanges() is called?


I'm adding a "WhatIf" switch (inspired by the Powershell -WhatIf switch) to my app, that just simulates data processing, without persisting any actual data changes back to the EF storage.

The way I hoped to implement this is simply by adding a check before calling SaveChanges(), like this:

if (WhatIf == false)
  efEntities.SaveChanges()

This way, the rest of the application can make changes as normal, and so long as SaveChanges() is never called, I don't have to worry about any changes being made accidentally.

Would this work? I'm worried that SaveChanges will get called by other parts of EF, such as Dispose, or something like that?

Thanks!


Solution

  • It will work. EF doesn't call SaveChanges itself. Developer is always responsible for persisting changes.