Entity Framework Core Interview Question #111

What is ExecuteUpdate in EF Core?

Change Tracking, Updates, Transactions & Concurrency Mid-Level Intermediate

Quick Interview Answer

ExecuteUpdate performs a set-based database UPDATE directly from a LINQ query without first loading matching entities into the change tracker.

Detailed Explanation

ExecuteUpdate performs a set-based database UPDATE directly from a LINQ query without first loading matching entities into the change tracker.

It is useful for bulk-style updates such as changing a status for many rows. Because tracked entities are bypassed, normal tracked-entity state and SaveChanges processing are not used for that operation.

Developers must account for any already tracked instances whose in-memory values may become stale.

Code Example

await db.Products
    .Where(p => p.StockQuantity == 0)
    .ExecuteUpdateAsync(setters => setters
        .SetProperty(p => p.IsAvailable, false));