Because in the previous version, Entity Framework Core (EF Core) could not efficiently insert, modify and delete data in batches, so I developed Zack.EFCore.Batch, an open-source project, which was quite popular and obtained more than 400 stars.
Since .NET 7, EF Core has built-in support for the efficient batch updating and deletion of data in Entity Framework Core 7. See this document for details https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew?WT.mc_id=DT-MVP-5004444#executeupdate-and-executedelete-bulk-updates So my open source project will no longer provide support for bulk updating and deletion of data in. NET 7. However, since Entity Framework Core still does not provide efficient bulk data insertion, I upgraded this open-source project to . NET 7, thus continuing to provide EF Core with the ability to efficiently bulk insert data.
Why did I develop this feature?
The AddRange() method can be used to batch insert data in Entity Framework Core. However, the data added by AddRange() is still inserted into the database by using INSERT statements one by one, which is inefficient. We know that SqlBulkCopy can quickly insert a large amount of data to SQLServer database, because SqlBulkCopy can pack multiple data into a packet and send it to SQLServer, so the insertion efficiency is very high. MySQL, PostgreSQL, and others have similar support.
Of course, using SqlBulkCopy to insert data directly requires the programmer to fill the data to the DataTable, perform column mapping, and handle ValueConverter and other issues, which is troublesome to use. Therefore, I encapsulated these capabilities to make it easier for EF Core developers to insert data in a model-oriented manner.
This library currently supports MS SQLServer, MySQL, and PostgreSQL databases.
Comparison of performance
I did a test of inserting 100,000 pieces of data with SQLServer database, and the insertion took about 21 seconds with AddRange(), compared to only about 5 seconds with my open-source project.
How to use?
Older versions of the library also support it. NET 5, and 6 version, such as specific usage see https://github.com/yangzhongke/Zack.EFCore.Batch, the following statement is for . NET 7.
First, install Nuget package:
SQLServer: Install-Package Zack.EFCore.Batch.MSSQL_NET7
MySQL: Install-Package Zack.EFCore.Batch.MySQL.Pomelo_NET7
Postgresql: Install-Package Zack.EFCore.Batch.Npgsql_NET7
You can then use the extension method BulkInsert for DbContext provided by my project to do bulk data insertion, as follows:
List<Book> books = new List<Book>();
for (int i = 0; i < 100; i++)
{
books.Add(new Book { AuthorName = "abc" + i, Price = new Random().NextDouble(), PubTime = DateTime.Now, Title = Guid.NewGuid().ToString() });
}
using (TestDbContext ctx = new TestDbContext())
{
ctx.BulkInsert(books);
}
GitHub repository: https://github.com/yangzhongke/Zack.EFCore.Batch
I hope this library helps.