3

According to this, bulk insert in Entity can be made using the following code:

 var customers = GetCustomers();   
 db.Customers.AddRange(customers);   
 db.SaveChanges();  

I used SQL Profiler to verify how many insert queries were executed and I saw there was an insert for each element of the list.

enter image description here

Why?

Luis Teijon
  • 4,769
  • 7
  • 36
  • 57
  • Are you seeing this behavior even after using that package? – Chetan Apr 02 '17 at 03:21
  • Yes, I have only tried the insert and it didn't work. Maybe I'm doing something wrong because I added the package and it didn't change anything. The code is the same. – Luis Teijon Apr 02 '17 at 03:27
  • That post you linked has more code in it than you have included here. For one thing, a custom DataContext with a DBSet is used. Have you followed the post *exactly?* Are you using Entity Framework 6, as specified by the post? – Robert Harvey Apr 02 '17 at 03:34
  • There is a sample solution [here](https://code.msdn.microsoft.com/Entity-Framework-Batch-994cd739/view/SourceCode#content). You should try that before declaring that it doesn't work. – Robert Harvey Apr 02 '17 at 03:38
  • I'm also using a custom Context that inherits from DbContex and contains a DbSet. – Luis Teijon Apr 02 '17 at 03:38
  • Try the sample solution. – Robert Harvey Apr 02 '17 at 03:38
  • That's the same example – Luis Teijon Apr 02 '17 at 03:44
  • 1
    No. *The exact solution,* file for file. Byte for byte. Either it works, and you're done, or it doesn't work and you file a bug report with Microsoft. – Robert Harvey Apr 02 '17 at 03:45
  • Hehehe, Ok, I will try using the exact same example. I will let you know. Thanks! – Luis Teijon Apr 02 '17 at 03:47
  • 1
    The thing is that linked article is misleading: they don't insert in bulk, rather using standard API. Only then they install the package and use its API to update in bulk, etc. – abatishchev Apr 02 '17 at 04:01
  • Also see [tag:entity-framework-extended] wiki: "Entity Framework Extended provides batch *updates, deletes and queries* for Entity Framework". – abatishchev Apr 02 '17 at 04:05

2 Answers2

3

That's how EF6 does "bulk" insert, it doesn't do in bulk, rather row by row. As a result performance sucks.

Use EF.BulkInsert or EFUtilities instead.

Kevin Doyon
  • 3,464
  • 2
  • 33
  • 38
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • The post he linked states that `EntityFramework.Extended` is being used. See https://code.msdn.microsoft.com/Entity-Framework-Batch-994cd739/sourcecode?fileId=150452&pathId=2085240737 – Robert Harvey Apr 02 '17 at 03:37
  • What does your update mean? It doesn't work because he isn't really using EF.Extended, or that he doesn't need EF.Extended at all? – Robert Harvey Apr 02 '17 at 03:40
  • That's the point. I'm not really sure if I'm using EF.Extended. I installed it but the code does not reference EF.Extended anyware. – Luis Teijon Apr 02 '17 at 03:42
  • From [EF.Extended's readme](https://github.com/loresoft/EntityFramework.Extended/blob/master/readme.md) I don't see it offers bulk insert functionality in the first place. – abatishchev Apr 02 '17 at 03:49
  • @LuisTeijon: my recommendation is to use EF.BulkInsert. It doesn't support other operations in bulk, i.e. no update, delete. However insert works perfectly. In my previous project we were inserting thousands of rows, several million per month. Without issues. It also support `SqlBulkCopyOptions` what is important if you want to check/enforce constraints. – abatishchev Apr 02 '17 at 03:52
  • 1
    DbSet.AddRange() also does one insert for each element in the list (except in EF Core) – Carlos Jul 09 '18 at 11:14
  • Bulk ops are supported with [EFCore.BulkExtensions](https://github.com/borisdj/EFCore.BulkExtensions) Disclaimer: I'm the author. – borisdj May 08 '23 at 09:13
3

AddRange

Add range doesn't perform a BulkInsert, it simply DetectChanges once after all entities are added to the set.

The DetectChange method can be VERY slow.

See: Entity Framework - DetectChanges Performance

As you noticed, it saves entities one by one in the database which is INSANELY slow.

EF.Extended

This library is not longer supported, and there is no Bulk Insert feature.

Bulk Insert Library

There is three major library supporting Bulk Insert:

Be careful, both free libraries don't support all inheritances and associations.


Disclaimer: I'm the owner of the project Entity Framework Extensions

In addition of Bulk Insert, this library allows you to perform all bulk operations:

  • BulkSaveChanges
  • BulkInsert
  • BulkUpdate
  • BulkDelete
  • BulkMerge
  • BulkSynchronize

Example:

// Easy to use
context.BulkSaveChanges();

// Easy to customize
context.BulkSaveChanges(bulk => bulk.BatchSize = 100);

// Perform Bulk Operations
context.BulkDelete(customers);
context.BulkInsert(customers);
context.BulkUpdate(customers);

// Customize Primary Key
context.BulkMerge(customers, operation => {
   operation.ColumnPrimaryKeyExpression = 
        customer => customer.Code;
});
Jonathan Magnan
  • 10,874
  • 2
  • 38
  • 60
  • What if I try creating a Stored Procedure to achieved the bulk insert? I will be calling the stored procedure from EntityFramework. – Luis Teijon Apr 02 '17 at 15:28
  • 1
    Stored Procedure will not be as fast as any of Bulk Insert Library and will require more time on your part, but you will get for sure performance enhancement over standard way to insert with Entity Framework. – Jonathan Magnan Apr 02 '17 at 16:44