Menu

SaveChanges shouldn't take four seconds to insert 100,000 rows
📰

SaveChanges shouldn't take four seconds to insert 100,000 rows

DEV Community: dotnet·Daniel Davis·6 days ago
#tRM8DO8R
#devtodotnet#devto#dev#sqlbulkpro#insert
Reading 0:00
15s threshold

If you have ever watched an import job stall after AddRange and SaveChanges , you already know the feeling. EF Core is doing honest work: it tracks every entity, generates commands, and talks to SQL Server more than you wanted. That is the right tradeoff when you are saving a cart or a user profile. It is a painful one when you are writing thousands or hundreds of thousands of rows.

I also know the other path by heart: SqlBulkCopy into a temp table, a MERGE with HOLDLOCK so two imports do not race, a transaction you actually commit, and a decision about identity values. It works. It is also easy to get slightly wrong.

So I built SqlBulkPro : a small .NET 8/9 library that uses SqlBulkCopy and MERGE under the hood and gives you one method for the usual cases. It works with EF Core and with Dapper / ADO.NET.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.