Bulk imports take minutes when inserting millions of rows. How can `SqlBulkCopy` or EFCore.BulkExtensions help while staying transactional?

.NET interview question for Advanced practice.

Answer

Map CSV rows to DTOs and feed them to SqlBulkCopy or EFCore.BulkExtensions' BulkInsertAsync, wrapping the call in a transaction so either the whole batch succeeds or none of it does. Keep ordinary EF inserts for small datasets to avoid unnecessary dependencies.

Explanation

SqlBulkCopy uses TDS streaming, which bypasses per-row INSERT statements.

Related Questions