Batch SaveChanges

Entity Framework Batch SaveChanges

Description

The Entity Framework BatchSaveChanges method is the fastest way to save a few or hundreds of entities.

The same SQL in the same order as SaveChanges is used but batched in fewer commands to increase the performance.

var affectedRows = context.BatchSaveChanges();

Try it

Performance Comparison

On average, people report a performance improvement of 500%.

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?

  • Entity Framework 6
  • SQL Server/Azure

Getting Started

Replace SaveChanges

Optimizing your performance is very easy, you simply need to replace SaveChanges by BatchSaveChanges:

// var affectedRows = context.SaveChanges();
var affectedRows = context.BatchSaveChanges();

Try it

Override SaveChanges

Or you can also override the SaveChanges method to call BatchSaveChanges method instead:

public class EntityContext : DbContext
{
    // ...code...
    
    public override int SaveChanges()
    {
        return this.BatchSaveChanges();
    }
}

Try it

FAQ

Why BatchSaveChanges is faster than SaveChanges?

The SaveChanges method makes 1 database round-trip for every 1 entity to save.

The BatchSaveChanges method makes 1 database round-trip for every 25 entities to save (Default Value).

So, if you need to save 100 entities:

  • The SaveChanges method will execute 100 SQL commands.
  • The BatchSaveChanges method will execute 4 SQL commands.

The same SQL in the same order as SaveChanges is used but batched in fewer commands to increase the performance.

Is BatchSaveChanges as fast as SaveChanges with one entity?

Under the hood, BatchSaveChanges uses SaveChanges to save 1 entity (so, as fast!), but will outperform it by batching commands in all other cases.

We always recommend to use BatchSaveChanges.

What is the difference between BatchSaveChanges and BulkSaveChanges?

For a few hundreds of entities or less, the BatchSaveChanges offers better performance than BulkSaveChanges.

However, the BulkSaveChanges is a more scalable method. So, it becomes faster than BatchSaveChanges when you start to save thousands of entities.

Documentation

BatchSaveChanges

Methods

Last updated: 2025-07-28
Author: