Updating multiple database records is a common requirement in business applications. An application may need to increase salaries for employees, update order statuses, deactivate expired subscriptions, or modify thousands of records based on a condition.
Entity Framework Core provides change tracking and the SaveChanges method for handling database modifications. This approach works well for normal application operations, but it can become inefficient when the same update must be applied to a large number of records.
EF Core provides ExecuteUpdate and ExecuteDelete to perform bulk database operations without loading every matching entity into memory. These methods allow developers to express updates and deletes through LINQ queries while letting the database execute the operation directly.
However, bulk operations have important differences from traditional change tracking. They execute immediately, do not synchronize tracked entities, and do not automatically provide the same concurrency handling as SaveChanges.
In this article, we will explore how to use ExecuteUpdate with transactions, implement concurrency checks, handle common problems, and apply these methods safely in production applications.
Understanding ExecuteUpdate in EF Core
ExecuteUpdate was introduced in EF Core 7.0. It allows developers to update database records directly without loading the matching entities into application memory.
Consider an application that manages employee records. Suppose the business requirement is to increase the salary of every employee in the Engineering department by 10 percent.
Using traditional change tracking, the application might load all matching employees, modify their salary properties, and call SaveChangesAsync.
var employees = await context.Employees
.Where(e => e.Department == "Engineering")
.ToListAsync();
foreach (var employee in employees)
{
employee.Salary *= 1.10m;
}
await context.SaveChangesAsync();This code works, but it loads every matching employee into memory and tracks each entity. EF Core then generates database update statements for the modified records.
With ExecuteUpdateAsync, the same operation can be performed directly in the database.
var affectedRows = await context.Employees
.Where(e => e.Department == "Engineering")
.ExecuteUpdateAsync(setters =>
setters.SetProperty(
e => e.Salary,
e => e.Salary * 1.10m));EF Core translates the LINQ query into a database update statement. The database performs the calculation and updates the matching rows without materializing employee objects.
This approach reduces application-side memory usage and avoids change-tracking overhead.
Understanding ExecuteDelete
EF Core also provides ExecuteDelete for deleting multiple records directly from the database.
For example, an application may need to remove expired temporary records.
The traditional approach would load the records and mark them for deletion:
var expiredRecords = await context.TemporaryRecords
.Where(r => r.ExpiresAt < DateTime.UtcNow)
.ToListAsync();
context.TemporaryRecords.RemoveRange(expiredRecords);
await context.SaveChangesAsync();Using ExecuteDeleteAsync, the operation becomes:
var deletedRows = await context.TemporaryRecords
.Where(r => r.ExpiresAt < DateTime.UtcNow)
.ExecuteDeleteAsync();The database deletes all matching records through a single SQL command.
Both ExecuteUpdate and ExecuteDelete work with relational database providers. They are useful when the required operation can be expressed as a database-level update or delete.
Updating Multiple Properties With ExecuteUpdate
An update operation often needs to modify more than one property.
For example, when an employee receives a promotion, the application may need to update the salary, job title, and modification timestamp.
ExecuteUpdateAsync supports multiple SetProperty calls in the same operation.
var affectedRows = await context.Employees
.Where(e => e.Department == "Engineering"
&& e.YearsOfExperience >= 5)
.ExecuteUpdateAsync(setters => setters
.SetProperty(e => e.Salary, e => e.Salary + 15000m)
.SetProperty(e => e.JobTitle, "Senior Developer")
.SetProperty(e => e.UpdatedAt, DateTime.UtcNow));EF Core generates one update command for the matching records. The database updates all three properties in that command.
The values can be constants or expressions based on existing database values, provided the expressions can be translated by the database provider.
This is useful for operations such as percentage increases, status changes, counter updates, and timestamp modifications.
Why Transactions Are Important for Bulk Updates
One of the most important differences between ExecuteUpdate and SaveChanges is transaction behavior.
A single ExecuteUpdateAsync operation is executed immediately. EF Core does not automatically create an explicit transaction that groups multiple separate bulk operations together.
Consider an order-processing workflow that performs two updates:
Mark an order as processed.
Reduce the available inventory.
await context.Orders
.Where(o => o.Id == orderId)
.ExecuteUpdateAsync(setters =>
setters.SetProperty(o => o.Status, "Processed"));
await context.Products
.Where(p => p.Id == productId)
.ExecuteUpdateAsync(setters =>
setters.SetProperty(
p => p.Stock,
p => p.Stock - quantity));If the first operation succeeds but the second operation fails, the order may remain marked as processed even though the inventory was not updated.
This creates inconsistent business data.
A database transaction allows multiple operations to succeed or fail as one logical unit.
Implementing Bulk Updates With an Explicit Transaction
EF Core provides transaction support through Database.BeginTransactionAsync.
The following example updates an order and reduces product inventory within the same transaction.
public async Task ProcessOrderAsync(
int orderId,
int productId,
int quantity)
{
await using var transaction =
await _context.Database.BeginTransactionAsync();
try
{
var orderRows = await _context.Orders
.Where(o => o.Id == orderId
&& o.Status == "Pending")
.ExecuteUpdateAsync(setters => setters
.SetProperty(o => o.Status, "Processed"));
if (orderRows == 0)
{
throw new InvalidOperationException(
"Order was not found or is not pending.");
}
var inventoryRows = await _context.Products
.Where(p => p.Id == productId
&& p.Stock >= quantity)
.ExecuteUpdateAsync(setters => setters
.SetProperty(
p => p.Stock,
p => p.Stock - quantity));
if (inventoryRows == 0)
{
throw new InvalidOperationException(
"Insufficient inventory.");
}
await transaction.CommitAsync();
}
catch
{
await transaction.RollbackAsync();
throw;
}
}The transaction ensures that both database updates are committed together.
If the inventory update fails, the exception causes the order update to be rolled back as well.
The example also checks the number of affected rows. This is important because a database update can execute successfully while matching no records.
In production applications, validate that the quantity is positive and ensure that the order and product identifiers belong to the intended business operation.
Understanding Change Tracking Limitations
ExecuteUpdate and ExecuteDelete do not interact with EF Core's change tracker.
This behavior is important when an application loads an entity and later updates the same record using a bulk operation.
Consider the following example:
var employee = await context.Employees
.FirstAsync(e => e.Id == 10);
await context.Employees
.Where(e => e.Id == 10)
.ExecuteUpdateAsync(setters =>
setters.SetProperty(
e => e.Salary,
e => e.Salary + 5000m));
employee.Salary += 2000m;
await context.SaveChangesAsync();
Join the conversation! Your thoughts help the community grow.