DbUpdateConcurrencyException when updating an entity

  • Error

Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: 'The database operation was expected to affect 1 row(s), but actually affected 0 row(s); data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.'
  • Code before

  foreach (var entity in entities)
  {
     ctx.Entry(entity).State = EntityState.Modified;
  }
  ctx.SaveChanges();

The problem was that some of the entities, that were residing in memory, were modified and saved in the database while their copy in memory were not updated. When savechanges trigerred there was a mismatch of rowversion, therefore the DbUpdateConcurrencyException.

  • Solution

1.

ctx.Entities.Where(x => x.Id == entity.Id).Update(x => new Entity()
            {
                SomeProperty = entity.SomeProperty,
                SomeProperty1 = entity.SomeProperty1
            });
 foreach (var entity in entities)
 {
    var dbentity = ctx.Entities.Where(x => x.Id == entity.Id).FirstOrDefault();
	//the property you want to update
    if (dbentity.SomeIndex != entity.SomeIndex)
    {
	  dbunit.SomeIndex = entity.SomeIndex;
    }
 }
 ctx.SaveChanges();