Entity Framework Extensions Bulk SaveChanges

Description

The EF BulkSaveChanges extension method execute bulk operations from entries to save.

// Easy to use
context.BulkSaveChanges();

// Easy to customize
context.BulkSaveChanges(options => options.BatchSize = 100);

Try it

Performance Comparison

Operations 1,000 Entities 2,000 Entities 5,000 Entities
SaveChanges 1,200 ms 2,400 ms 6,000 ms
BatchSaveChanges 150 ms 225 ms 500 ms

Try this benchmark online

HINT:

A lot of factors might affect the benchmark time such as index, column type, latency, throttling, etc.

What is supported?

  • All Entity Framework versions (EF4, EF5, EF6, EF Core, EF Classic)
  • All Inheritances (TPC, TPH, TPT)
  • Complex Type/Owned Entity Type
  • Enum
  • Value Converter (EF Core)
  • And more!

Advantages

  • Easy to use
  • Flexible
  • Increase performance
  • Increase application responsivness
  • Reduce database load
  • Reduce database round-trips

FAQ

Why BatchSaveChanges is faster than SaveChanges?

The SaveChanges method makes it quite slow/impossible to handle scenario that requires to save a lot of entities due to the number of database round-trips required. The SaveChanges perform one database round-trip for every entity to insert. So if you need to insert 10,000 entities, 10,000 database round-trips will be performed which is INSANELY slow.

The BulkSaveChanges in counterpart requires the minimum database round-trips as possible. By using Bulk Operations, fewer command is executed which lead to better performance.

When should I use BulkSaveChanges over SaveChanges?

Whenever you have more then one entities to saves. The BulkSaveChanges is almost as fast as the SaveChanges for 1 entities, but become way faster as the number of entities to save growth.

When should I use BulkSaveChanges over BatchSaveChanges?

BatchSaveChanges become slower and slower in comparisons to BulkSaveChanges when the number of entities to save growth due to the ChangeTracker.

After a few thousands of Entities, we recommend using BulkSaveChanges which is a more scalable solution.

Documentation

BatchSaveChanges

Methods
Name Description Example
BulkSaveChanges() Saves all changes made in this context to the underlying database by executing bulk operations. Try it
BulkSaveChanges(Action<BulkOperation> bulkOperationFactory) Saves all changes made in this context to the underlying database by executing bulk operations. Try it
BulkSaveChanges(bool useEntityFrameworkPropagation) Saves all changes made in this context to the underlying database by executing bulk operations. Try it
BulkSaveChanges(bool useEntityFrameworkPropagation, Action<BulkOperation> bulkOperationFactory) Saves all changes made in this context to the underlying database by executing bulk operations. Try it
BulkSaveChangesAsync(...) Saves all changes asynchronously made in this context to the underlying database by executing bulk operations.
Options

More options can be found here:

In this Page