4

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?

SB2055
  • 12,272
  • 32
  • 97
  • 202

2 Answers2

5

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);  
Northern25
  • 71
  • 1
  • 2
4

You can use the following library:

https://github.com/MikaelEliasson/EntityFramework.Utilities

It works well for simple bulk inserts and updates.

You should also look at the following post if you want to find out about other options to achieve bulk insert:

Fastest Way of Inserting in Entity Framework

Avi K.
  • 1,734
  • 2
  • 18
  • 28