thilanka1989
thilanka1989

Reputation: 77

Batch Update a List of Record Entitiy Framework

I have a list of updated objects. And I tried to update all records with only one database call.(one context.SaveChanges() command). This programme runs inside a windows service. My problem is instead of updating I am multiplicating all records on the database and adding duplicates. Following is my code.

//adding the list of updated customer objects to the list
context_CLTUS.Customer_Updates.AddRange(list.customerUpdateList);
foreach (var j in list.customerUpdateList) 
{
    //calling entity state modify for each record
    context_CLTUS.Entry(j).State = EntityState.Modified;
}
//save updated list in one db call.
context_CLTUS.SaveChanges();

I cannot clarify why all records are duplicating instead of updating. Could you please explain and help me on this. Thank you.

Upvotes: 1

Views: 564

Answers (1)

NicoXiang
NicoXiang

Reputation: 436

Refer to this answer

You can try these solutions :

  1. Swap update operation in a TransactionScope.

  2. Set Configuration.AutoDetectChangesEnabled = false.

  3. Try Entity Framework Plus, a high-performance enhancement library.

Upvotes: 1

Related Questions