Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bulk Insert with Entity Framework 6

I'm aware that there are some commercial libraries and that there's AddRange, but AFAIK AddRange does piecemeal INSERTs under the hood.

I'm looking for a free utility that I can use to add a collection of new entities all at the same time - does one exist for EF6?

like image 706
SB2055 Avatar asked Jul 26 '26 20:07

SB2055


1 Answers

I would suggest you take a look at N.EntityFramework.Extension. It is a basic bulk extension framework for EF 6 that is available on Nuget and the source code is available on Github under MIT license.

Install-Package N.EntityFramework.Extensions

https://www.nuget.org/packages/N.EntityFramework.Extensions

Once you install it you can simply use BulkInsert() method directly on the DbContext instance. It support BulkDelete, BulkInsert, BulkMerge and more.

BulkDelete()

var dbcontext = new MyDbContext();  
var orders = dbcontext.Orders.Where(o => o.TotalPrice < 5.35M);  
dbcontext.BulkDelete(orders);

BulkInsert()

var dbcontext = new MyDbContext();  
var orders = new List<Order>();  
for(int i=0; i<10000; i++)  
{  
   orders.Add(new Order { OrderDate = DateTime.UtcNow, TotalPrice = 2.99 });  
}  
dbcontext.BulkInsert(orders);  
like image 72
Northern25 Avatar answered Jul 30 '26 10:07

Northern25



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!