When using EF Core to update data, the typical process involves several steps:
Load entities from the database into memory This is usually done using methods like
Find
,FirstOrDefault
, orWhere
on theDbSet
class.Modify the entityβs properties Once the entities are loaded, you update their properties as needed.
Call
SaveChanges()
After making your changes, you callSaveChanges
on theDbContext
to persist them to the database.
Hereβs a basic example that updates multiple entities:
var importCustomers = new List<Customer>
{
new Customer { ID = 1, Name = "ZZZ Projects", City = "New York" },
new Customer { ID = 2, Name = "Jonathan Magnan", City = "Montreal" }
// Add more customers as needed
};
var importCustomersDict = importCustomers.ToDictionary(x => x.ID);
using (var context = new MyDbContext())
{
// 1. Load entities from the database into memory
var customers = context.Customers
.Where(x => importCustomersDict.ContainsKey(x.ID))
.ToList();
// 2. Modify the entity's properties
foreach (var customer in customers)
{
var importCustomer = importCustomersDict[customer.ID];
customer.Name = importCustomer.Name;
customer.City = importCustomer.City;
}
// 3. Call SaveChanges
context.SaveChanges();
}
This approach works fine for a small number of entities β but what if you need to update 10,000 customers?
Thatβs where the BulkUpdate method from the Entity Framework Extensions library shines. Itβs faster, cleaner, and skips all the heavy lifting:
- β No need to load entities first
- β No need to manually update properties
- β Just update your entities directly
By using the BulkUpdate method from ZZZ Projects, you can skip steps 1 and 2 β and update your entities directly in the database:
// @nuget: Z.EntityFramework.Extensions.EFCore
using Z.EntityFramework.Extensions;
var importCustomers = new List<Customer>
{
new Customer { ID = 1, Name = "ZZZ Projects", City = "New York" },
new Customer { ID = 2, Name = "Jonathan Magnan", City = "Montreal" }
// Add more customers as needed
};
using (var context = new MyDbContext())
{
// Easy to use
context.BulkUpdate(importCustomers, options =>
options.ColumnInputExpression = x => new { x.Name, x.City });
}
using (var context = new MyDbContext())
{
// Supports async
await context.BulkUpdateAsync(importCustomers, options =>
options.ColumnInputExpression = x => new { x.Name, x.City });
}
Youβve just turned an inefficient, multi-step update into a single line of code that any developer can understand:
// @nuget: Z.EntityFramework.Extensions.EFCore
using Z.EntityFramework.Extensions;
context.BulkUpdate(importCustomers, options =>
options.ColumnInputExpression = x => new { x.Name, x.City });
In this example, all imported customers will be updated, and only the Name
and City
properties will be affected.
π Why Use the Bulk Update Method from Entity Framework Extensions?
The bulk update method from Entity Framework Extensions is the recommended solution for EF Core when you need to update large sets of data efficiently.
Entity Framework Extensions is the most popular and most complete library available, with over 50 million downloads and more than 5,000 paid customers. Bulk updating in EF Core with Entity Framework Extensions is up to 4x faster, reducing execution time by 75% (Online Benchmark).
It helps you optimize database performance by reducing the number of queries, making your code cleaner, and giving you full control over how updates are applied:
- β Update the way you want β Choose which properties to update, skip null values, apply conditions, and more.
- β Extremely fast β Update thousands or even millions of rows in seconds. See the Performance Comparison.
- β No need to load entities β Save time and memory by skipping entity tracking.
- β Flexible with hundreds of options β Fine-tune everything: batch size, conditions, behaviors, and more. Explore the Bulk Update Options.
- β Supports all major providers β SQL Server, MySQL, MariaDB, Oracle, PostgreSQL, SQLite.
- β Most advanced EF Core library β Supports inheritance, owned types, complex types, and more.
π οΈ Commonly Used Options with the the Bulk Update Method from Entity Framework Extensions
Here are some of the most frequently used options with the BulkUpdate
method from the Entity Framework Extensions library:
ColumnPrimaryKeyExpression
β Choose which properties to use as the primary key by providing a lambda expression. Only rows that match the key will be updated.ColumnInputExpression
β Select which properties should be updated by using a lambda expression. All other properties will be ignored.IgnoreOnUpdateExpression
β Specify which properties should be ignored during the update using a lambda expression. All other properties will be included.UpdateMatchedAndConditionExpression
β After matching rows by primary key, you can add extra conditions using a lambda expression. All specified property values must match between the entity and the database for the update to happen.UpdateMatchedAndOneNotConditionExpression
β Similar to the above, but only one of the specified property values needs to be different between the entity and the database for the row to be updated.IncludeGraph
β Set this totrue
if you want to update both the main entities and their related entities. For example, if you pass a list ofOrder
that includesOrderItem
, both will be updated. β οΈ Be careful β if you want to apply specific options to a related entity type, youβll need to configure them usingIncludeGraphBuilder
.UseRowsAffected
β Set this totrue
if you want to know how many rows were affected after the update. You can then accessResultInfo.RowsAffected
orResultInfo.RowsAffectedUpdated
. π Learn more
π¦ How to Install Entity Framework Extensions
To use the bulk update method, install the Z.EntityFramework.Extensions.EFCore NuGet package:
PM> NuGet\Install-Package Z.EntityFramework.Extensions.EFCore
> dotnet add package Z.EntityFramework.Extensions.EFCore
Entity Framework Extensions uses EF Core pinned versioning, so make sure the first digit of the package version matches your EF Core version. For example:
- If you're using
EF Core 9
, use versionv9.x.y.z
- If you're using
EF Core 8
, use versionv8.x.y.z
- And so on...
Looking for a different version, like BulkUpdate
for EF6?
π Download it here
β Final Thoughts
If you're still updating large numbers of rows using regular EF Core logic, it's time to make your life easier.
With just one line of code, the bulk update method from Entity Framework Extensions lets you:
- Skip unnecessary steps
- Dramatically boost performance
- Customize everything to fit your needs
Whether you're dealing with 10 or 10 million rows, the bulk update method from Entity Framework Extensions gives you the speed, control, and simplicity you've been looking for.
Give it a try and see why over 5,000 companies already trust Entity Framework Extensions to handle their bulk operations. Once you use it, you wonβt want to go back!
π External Links
Looking for more resources? Here are some helpful links to learn more about the BulkUpdate
method and Entity Framework Extensions, explore its documentation, and download the right version for your project.