.NET Entity Framework Insert vs Bulk Insert

.net c# entity-framework


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?

6/12/2013 2:31:02 PM

Accepted Answer

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.

6/12/2013 2:36:59 PM

Popular Answer

There is oportunity for several improvements in Entity Framework:


yourContext.Configuration.AutoDetectChangesEnabled = false;
yourContext.Configuration.ValidateOnSaveEnabled = false;

Do SaveChanges() in packages of 100 inserts... try with 1000 and see the changes.

Since during all this inserts, the context is the same, you can rebuild your context object every 1000 inserts. var yourContext = new YourContext();

Doing this improvements in an importing data process of mine, took it from 7 minutes to 6 seconds.

The actual numbers... could not be 100's o 1000's in your case... try it and tweek it.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow