EF Core Batch Update — Complete Guide
In this tutorial, you'll learn about EF Core Batch Update. We cover key concepts, practical examples, and best practices.
You need to update the Category of all products where Price > 100. Without batch operations, you load every product, modify each entity, and call SaveChanges — sending one UPDATE per product. EF Core batch update (EF Core 7+) generates a single SQL UPDATE.
Wrong
var products = await db.Products.Where(p => p.Price > 100).ToListAsync();
foreach (var p in products)
{
p.Category = "Premium";
}
await db.SaveChangesAsync();
Output: SELECT * FROM Products WHERE Price > 100 followed by N individual UPDATE statements. Memory usage grows with the number of products.
Right (EF Core 7+)
await db.Products
.Where(p => p.Price > 100)
.ExecuteUpdateAsync(setters => setters
.SetProperty(p => p.Category, "Premium")
.SetProperty(p => p.LastModified, DateTime.UtcNow));
Output: Single SQL: UPDATE Products SET Category = 'Premium', LastModified = '2026-06-24' WHERE Price > 100. No data loaded into memory.
ExecuteUpdate supports any IQueryable, so you can compose complex filters:
await db.Orders
.Where(o => o.Date < cutoff && o.Status == "Pending")
.ExecuteUpdateAsync(s => s.SetProperty(o => o.Status, "Expired"));
Prevention
- Use
ExecuteUpdateAsyncfor bulk updates in EF Core 7+. - Use
ExecuteDeleteAsyncfor bulk deletes. - Use
ExecuteUpdatewith multipleSetPropertycalls for multi-column updates. - Compose
Whereclauses for targeted updates. - Use
ExecuteUpdatein data maintenance and background jobs. - Fall back to
ExecuteSqlRawfor complex updates EF Core cannot express. - Test that the generated SQL matches your expectations.
Common Mistakes with core batch update
- Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
These mistakes appear frequently in real-world EF code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Batch operations power DodaTech's data maintenance and bulk processing pipelines. For more EF Core, visit DodaTech.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro