Entity Framework Core Interview Question #191

How should bulk imports be approached with EF Core?

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

Quick Interview Answer

For moderate imports, AddRange with sensible batching may be sufficient. Very large imports may require provider-specific bulk-copy tools, optimized SQL, or specialized libraries because normal change tracking and individual parameterized commands can become expensive. Validate data, define transaction boundaries, measure throughput, and avoid sacrificing integrity merely for speed.

Detailed Explanation

For moderate imports, AddRange with sensible batching may be sufficient. Very large imports may require provider-specific bulk-copy tools, optimized SQL, or specialized libraries because normal change tracking and individual parameterized commands can become expensive. Validate data, define transaction boundaries, measure throughput, and avoid sacrificing integrity merely for speed.

Code Example

db.Products.AddRange(batch);
await db.SaveChangesAsync(cancellationToken);
db.ChangeTracker.Clear();