1

I have to update 5000 rows in database. What I did is load them from db to entity context, then changed the properties values, and after that call SaveChanges(), but the problem is that when I call SaveChanges(), update statements are executed one by one, and every update statement is one trip to database which takes about 40ms, 40ms x 5000 records is 200 seconds... Is there any way to send bulk update to databse, many updates in one databse trip...

Aleksa
  • 2,608
  • 3
  • 26
  • 39
  • 2
    It would probably be better to not use EF for this particular task. You can, however, get the underlying database connection from EF, and then just perform your own SQL. – Matthew Aug 10 '15 at 14:55
  • 2
    Duplicate of several other questions i.e. http://stackoverflow.com/questions/5940225/fastest-way-of-inserting-in-entity-framework – bubi Aug 10 '15 at 15:18

1 Answers1

0

I found a solution. Here is the link to Entity extension for bulk inserts: https://efbulkinsert.codeplex.com/ If you need bulk update you can do bulk insert into some temp table and then call stored procedure that does update through join with that temp table.

Aleksa
  • 2,608
  • 3
  • 26
  • 39
  • 1
    That extension doesn't do what you think it does. It converts entities to DataTables then uses SqlBulkCopy to do the actual bulk insertion. Better to avoid EF entirely and load the datatables directly. EF adds nothing but overhead to this scenario – Panagiotis Kanavos Aug 12 '15 at 11:52
  • Actually it does what I think and what I need. It takes list of entities as parameter and insert it very quickly to database as bulk insert. – Aleksa Aug 12 '15 at 12:44