Entity Framework Core Interview Question #189

How should large batch operations be designed with EF Core?

Testing, Production Practices & Senior Real-World Scenarios Senior Advanced

Quick Interview Answer

Process large workloads in bounded batches rather than loading or tracking an entire dataset at once. Use set-based ExecuteUpdate or ExecuteDelete where suitable, project only needed data, and periodically create a fresh context or clear tracking when processing tracked entities in large loops. Measure transaction size, locking, log growth, and memory usage.

Detailed Explanation

Process large workloads in bounded batches rather than loading or tracking an entire dataset at once. Use set-based ExecuteUpdate or ExecuteDelete where suitable, project only needed data, and periodically create a fresh context or clear tracking when processing tracked entities in large loops. Measure transaction size, locking, log growth, and memory usage.

Code Example

const int batchSize = 500;
var rows = await db.Items
    .AsNoTracking()
    .OrderBy(x => x.Id)
    .Take(batchSize)
    .ToListAsync(cancellationToken);