Phase 26 of 30 · Topic 26.3

Bulk Operations: `ExecuteUpdateAsync` & `ExecuteDeleteAsync` (.NET 7/8/9)

1Concept

Prior to .NET 7, updating 10,000 records required loading them into RAM and tracking each entity. `ExecuteUpdateAsync` and `ExecuteDeleteAsync` execute direct `UPDATE` / `DELETE` SQL statements on the database server in a single roundtrip.

2Architecture Diagram

Legacy EF Update:
Load 10,000 rows into RAM ──> Mutate properties ──> Send 10,000 UPDATE statements (Slow!)

Modern ExecuteUpdateAsync (.NET 8/9):
ExecuteUpdateAsync(s => s.SetProperty(u => u.IsActive, false))
       │
       ▼ Single SQL Command:
UPDATE Users SET IsActive = 0 WHERE LastLogin < '2025-01-01' (Instantaneous!)

3Code Example

C# 13 & .NET 9
using System;

public class BulkOperationsConceptDemo
{
    public static void Main()
    {
        Console.WriteLine("High-speed direct database bulk update:");
        Console.WriteLine("await context.Users");
        Console.WriteLine("    .Where(u => u.AccountStatus == "Suspended")");
        Console.WriteLine("    .ExecuteUpdateAsync(setters => setters");
        Console.WriteLine("        .SetProperty(u => u.IsActive, false)");
        Console.WriteLine("        .SetProperty(u => u.UpdatedAt, DateTime.UtcNow));");
    }
}

4Expected Output

High-speed direct database bulk update:
await context.Users
    .Where(u => u.AccountStatus == "Suspended")
    .ExecuteUpdateAsync(setters => setters
        .SetProperty(u => u.IsActive, false)
        .SetProperty(u => u.UpdatedAt, DateTime.UtcNow));

5Key Takeaways

  • `ExecuteUpdateAsync` executes directly on the database server with zero memory allocation in C#.
  • Does not invoke Change Tracker or interceptors.
  • Ideal for batch updates, status changes, and archiving.