Bulk UpdateEasily customize and optimize your entity updates in EF Core now
The BulkUpdate
method is the most flexible way to update your entities in EF Core. It allows you to customize how your entities will be updated, such as by specifying a custom key, updating only a few properties, and much more.
// Easy to use context.BulkUpdate(customers); // Easy to customize context.BulkUpdate(customers, options => options.IncludeGraph = true);
Online Example (EF Core) | Online Example (EF6)
Operations | 1,000 Entities | 2,000 Entities | 5,000 Entities |
---|---|---|---|
SaveChanges | 1,200 ms | 2,400 ms | 6,000 ms |
BulkUpdate | 80 ms | 110 ms | 170 ms |
Try it in EF Core | Try it in EF6
HINT:A lot of factors might affect the benchmark time such as index, column type, latency, throttling, etc.
The BulkUpdate
method is fast but also flexible to let you handle various scenarios in Entity Framework such as:
- Update and include/exclude properties
- Update with custom key
- Update with related child entities (Include Graph)
- Update with future action
- More scenarios
- All Entity Framework Core Version: EF Core 9, EF Core 8, EF Core 7, EF Core 6, EF Core 5, EF Core 3
- All Entity Framework Version: EF6, EF5, EF4
- All Inheritances (TPC, TPH, TPT)
- Complex Type/Owned Entity Type
- Enum
- Value Converter (EF Core)
- And more!
- Easy to use
- Flexible
- Increase performance
- Increase application responsiveness
- Reduce database load
- Reduce database round-trips
The BulkUpdate
and BulkUpdateAync
methods extend your DbContext
to let you update a large number of entities in your database.
context.BulkUpdate(customers); context.BulkUpdateAsync(customers, cancellationToken);
Try it in EF Core | Try it in EF6
The options
parameter lets you use a lambda expression to customize the way entities are updated.
context.BulkUpdate(customers, options => options.ColumnPrimaryKeyExpression = c => c.Code });
Try it in EF Core | Try it in EF6
Updating thousands of entities for a file importation is a typical scenario.
The SaveChanges
method makes it quite impossible to handle this kind of situation due to the number of database round-trips required. The SaveChanges
perform one database round-trip for every entity to update. So, if you need to update 10,000 entities, 10,000 database round-trips will be performed which is INSANELY slow.
The BulkUpdate
in counterpart requires the minimum database round-trips possible. For example, under the hood for SQL Server, a SqlBulkCopy
is performed first in a temporary table, then an UPDATE
from the temporary table to the destination table is performed which is the fastest way available.
You want to update your entities but only for specific properties.
IgnoreOnUpdateExpression
: This option lets you ignore properties that are auto-mapped.
context.BulkUpdate(customers, options => options.IgnoreOnUpdateExpression = c => new { c.ColumnToIgnore } );
Try it in EF Core | Try it in EF6
You want to update entities, but you don't have the primary key. The ColumnPrimaryKeyExpression
let you use as a key any property or combination of properties.
context.BulkUpdate(customers, options => options.ColumnPrimaryKeyExpression = c => c.Code);
Try it in EF Core | Try it in EF6
You want to update entities but also automatically insert related child entities.
IncludeGraph
: This option lets you automatically update all entities part of the graph.IncludeGraphBuilder
: This option lets you customize how to update entities for a specific type.
context.BulkUpdate(invoices, options => options.IncludeGraph = true);
Try it in EF Core | Try it in EF6
You want to update entities, but you want to defer the execution.
By default, BulkUpdate
is an immediate operation. That mean, it's executed as soon as you call the method.
FutureAction
: This option lets you defer the execution of a Bulk Update.
ExecuteFutureAction
: This option trigger and execute all pending FutureAction
.
context.FutureAction(x => x.BulkUpdate(customers)); context.FutureAction(x => x.BulkUpdate(invoices, options => options.IncludeGraph = true)); // ...code... context.ExecuteFutureAction();
Try it in EF Core | Try it in EF6
Hundreds of scenarios have been solved and are now supported.
The best way to ask for a special request or to find out if a solution for your scenario already exists is by contacting us: info@zzzprojects.com
Methods
Name | Description | Example |
---|---|---|
BulkUpdate<T>(items) |
Bulk update entities in your database. | EFCore / EF6 |
BulkUpdate<T>(items, options) |
Bulk update entities in your database. | EFCore / EF6 |
BulkUpdateAsync<T>(items) |
Bulk update entities asynchronously in your database. | EFCore / EF6 |
BulkUpdateAsync<T>(items, cancellationToken) |
Bulk update entities asynchronously in your database. | EFCore / EF6 |
BulkUpdateAsync<T>(items, options, cancellationToken) |
Bulk update entities asynchronously in your database. | EFCore / EF6 |
Options
More options can be found here:
- Audit
- Batch
- Column
- Context Factory
- Execute Event
- Identity
- Include Graph
- Key
- Logging
- Temporary Table
- Transaction
- Transient Error
- SQL Server
ZZZ Projects