user2206329
user2206329

Reputation: 2842

bulk update with EF, what is the best way?

I need to update in bulk, it is pretty much the below sql statement

update RECORDS set SOMEValue = 1 where ID in (1, 5, 3,6,7,9)

I have written the following which works

  //get all the records to update
    var COESRecord = context.COESDetails.Where(x => model.COESs.Contains(x.COESNo));           

    foreach (var COES in COESRecord)
    {
         COES.InspectorId = model.InspectorId;
         context.Entry(COES).State = EntityState.Modified;                
    }
    context.SaveChanges();

But the issue is when i am updating 1000 records it just takes a long time. Is this the best way to do it? or is there another way to do bulk updates?

Upvotes: 0

Views: 2203

Answers (2)

Jurica Smircic
Jurica Smircic

Reputation: 6455

If you want to stay away from SQL you can try with EntityFramework.Extended.

Provides support for writing LINQ like batch delete/update queries. I only tried it once, it worked nice, but not sure if i would use it in production.

Upvotes: 1

iamkrillin
iamkrillin

Reputation: 6876

There are two ways I can think of right off hand to achieve what you are seeking.

1) create a stored procedure and call it from your entity model.

2) Send the raw command text to the db, see this microsoft article

Upvotes: 0

Related Questions