How to Improve SaveChanges Performance?
The DbContext.SaveChanges is a poor choice for BULK operations as far as performance is concerned. Once you get beyond a few thousand records, the SaveChanges method really starts to break down.
StackOverflow Related Questions
- Entity framework performance issue, saveChanges is very slow
- Entity framework performance slow
- When should I call SaveChanges() when creating 1000’s of Entity Framework objects?
Entity Framework Extensions library adds the BulkSaveChanges extension method to the DbContext. It performs save operations 10 to 50 times faster.
All changes made in the context are persisted in the database but way faster by reducing the number of database round-trip required!
BulkSaveChanges supports everything:
- Association (One to One, One to Many, Many to Many, etc.)
- Complex Type
- Inheritance (TPC, TPH, TPT)
- Navigation Property
BulkSaveChanges is already very fast, but you can make it even faster by simply turning off the EntityFrameworkPropagation option.
When turning off this option, the library does no longer use the methods from Entity Framework but internal methods from our Entity Framework Extensions library.