c# - EF Codefirst Bulk Insert -


i need insert around 2500 rows using ef code first.

my original code looked this:

foreach(var item in listofitemstobeadded) {     //biz logic     context.mystuff.add(i); } 

this took long time. around 2.2 seconds each dbset.add() call, equates around 90 minutes.

i refactored code this:

var tempitemlist = new list<mystuff>(); foreach(var item in listofitemstobeadded) {     //biz logic     tempitemlist.add(item) } context.mystuff.tolist().addrange(tempitemlist); 

this takes around 4 seconds run. however, .tolist() queries items in table, extremely necessary , dangerous or more time consuming in long run. 1 workaround context.mystuff.where(x=>x.id = *empty guid*).addrange(tempitemlist) because know there never returned.

but i'm curious if else knows of efficient way to bulk insert using ef code first?

validation expensive portion of ef, had great performance improvements disabling with:

context.configuration.autodetectchangesenabled = false; context.configuration.validateonsaveenabled = false; 

i believe found in similar question--perhaps answer

another answer on question rightly points out if need bulk insert performance should @ using system.data.sqlclient.sqlbulkcopy. choice between ef , ado.net issue revolves around priorities.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -