Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Entity Framework Insert vs Bulk Insert

When I use my xxxContext object and issue several Adds to a table, then SaveChanges() how does the entity framework resolve this to SQL? Will it just loop doing insert into xxx or if there are hundreds of rows, is it smart enough to issue a Bulk insert command?

Bonus Question: If it doesn't issue the Bulk Insert is there a way to force it to so my DB performance isn't killed by separate inserts? Or to bulk to a temp table then merge to the original table like an Upsert?

like image 998
Kelly Avatar asked Jun 12 '13 14:06

Kelly


People also ask

What is bulk insert in Entity Framework?

The EF Bulk Insert feature let you insert thousands of entities in your database efficiently. This feature is provided by the library EF Extensions (Included with EF Classic). EF Extensions is used by over 2000 customers all over the world and supports all Entity Framework versions (EF4, EF5, EF6, EF Core, EF Classic).

How do I insert multiple rows in Entity Framework?

You can add multiple records or multiple objects using the AddRange method of DbSet as shown in the following code. The code creates a list of department objects and inserts two new departments to the list. We add the list to the context using the AddRange method.


1 Answers

The downfall of any ORM tool is that it is "chatty". Most times this is good enough. Sometimes it is not.

The short answer is "no".

Which is why I still sometimes pick IDataReader over EF or NHibernate, etc. And for bulk insert operations, I send xml to the stored procedure, and I shred it and bulk insert/update or merge from there.

So even when I use an ORM, I create a Domain Library that is not EF (or NHibernate) dependent......so I have a "safety valve" to by pass the ORM in certain situations.

like image 109
granadaCoder Avatar answered Oct 16 '22 14:10

granadaCoder