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?

11
  • Are you seeing this behavior even after using that package? Commented Apr 2, 2017 at 3: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. Commented Apr 2, 2017 at 3: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? Commented Apr 2, 2017 at 3:34
  • 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. Commented Apr 2, 2017 at 3:45
  • 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. Commented Apr 2, 2017 at 4:01

2 Answers 2

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;
});
Sign up to request clarification or add additional context in comments.

2 Comments

What if I try creating a Stored Procedure to achieved the bulk insert? I will be calling the stored procedure from EntityFramework.
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.
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.

7 Comments

The post he linked states that EntityFramework.Extended is being used. See code.msdn.microsoft.com/Entity-Framework-Batch-994cd739/…
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?
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.
From EF.Extended's readme I don't see it offers bulk insert functionality in the first place.
DbSet<T>.AddRange() also does one insert for each element in the list (except in EF Core)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.