Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF code first inserting many rows

I'm using EF code first and I need to insert many rows into a table, a couple of thousand records, I have in a generic list. Just doing a foreach loop and adding each entity to the table and finish with a SaveChanges() takes quite a while.

Is there a bulky way of doing this with EF or code first or should I do SqlCommand or SqlBulkCopy instead? Also, is there something I can switch off in EF code first to make the inserts faster?

Thanks for the help!

like image 288
Johan Danforth Avatar asked Nov 15 '22 02:11

Johan Danforth


1 Answers

You can call SaveChanges() less often.

However, nothing you can do with the EF will approach the performance of something like SqlBulkCopy.

like image 71
Craig Stuntz Avatar answered Dec 26 '22 07:12

Craig Stuntz