C#C
C#15mo ago
Ruttie

EFCore - When to save changes

I have an application that uses EFCore.
In this application, I have a method that gets some values from the database, and may or may not change them based on some conditions.
How should I handle the saving of changes in this scenario?

I've thought of a few solutions:
  • Meticulously try to only call SaveChanges if a values was changed (for example by having a local change boolean)
  • Simply always call SaveChanges at the end of the function
  • A hybrid approach, call DbContext.ChangeTracker.HasChanges() and only call SaveChanges if this returns true
What would be the best approach of these? (alternate approaches are also welcome)
Was this page helpful?