GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Entity Framework Extensions EF Extensions
  • Getting Started
  • Documentation
  • Online Examples
  • Contact
  • Buy
  • Download
  • Bulk Insert
  • Bulk Insert Optimized
  • Bulk Update
  • Bulk Delete
  • Bulk Merge
  • Bulk Synchronize
  • Bulk SaveChanges

This view

  • Original page
  • New address

GeezerVison is showing entityframework-extensions.net — Batch SaveChanges

  • Main Features
    • Bulk Extensions
    • Bulk Insert
    • Bulk Insert Optimized
    • Bulk Update
    • Bulk Delete
    • Bulk Merge
    • Bulk Synchronize
    • Bulk SaveChanges
    • WhereBulkContains
  • Single Methods
    • Single Insert
    • Single Update
    • Single Delete
    • Single Merge
  • Column Options
    • Input/Output/Ignore
    • Primary Key
    • Coalesce
    • Coalesce Destination
    • Matched and Condition
    • Matched and one NOT Condition
    • Matched and Formula
    • Delete Matched and Condition
    • Delete Matched and one NOT Condition
    • Delete Matched and Formula
  • Bulk Options
    • Bulk Options
    • Audit
    • Batch
    • Context Factory
    • Events
    • ExplicitValueResolutionMode
    • Identity
    • Include Graph
    • ForceValueGeneratedStrategy
    • Key
    • Logging
    • Model Caching
    • Rows Affected
    • Streaming
    • Temporary Table
    • Transaction
    • Transient Error
    • SQL Server
  • Batch Operations
    • Insert from Query
    • Update from Query
    • Delete from Query
  • Utilities
    • Batch SaveChanges
    • Delete ByKey
    • Delete Range ByKey
    • ToSelfHierarchyList
    • BulkRead
    • WhereBulkNotContains
    • WhereBulkContainsFilterList
    • WhereBulkNotContainsFilterList
    • WhereExistsBulkInsert
    • WhereNotExistsBulkInsert
  • Articles
  • Troubleshooting
  • Release Notes

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%.

Operations 50 Entities 200 Entities 500 Entities
SaveChanges 65 ms 250 ms 600 ms
BatchSaveChanges 25 ms 50 ms 125 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?

  • 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
Name Description Example
BatchSaveChanges() Saves all changes made in this context to the underlying database by combining SQL commands generated. Try it
BatchSaveChangesAsync() Saves all changes asynchronously made in this context to the underlying database by combining SQL commands generated. Try it
BatchSaveChangesAsync(cancellationToken) Saves all changes asynchronously made in this context to the underlying database by combining SQL commands generated. Try it

Last updated: 2025-07-28
Author: ZZZ Projects


Contents

  • Description
    • Performance Comparison
    • What is supported?
  • Getting Started
    • Replace SaveChanges
    • Override SaveChanges
  • FAQ
    • Why BatchSaveChanges is faster than SaveChanges?
    • Is BatchSaveChanges as fast as SaveChanges with one entity?
    • What is the difference between BatchSaveChanges and BulkSaveChanges?
  • Documentation
    • BatchSaveChanges
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.