AndersonCavalcante-5944 avatar image
0 Votes"
AndersonCavalcante-5944 asked Viorel-1 edited

C# Delete Operation - DbUpdateConcurrencyException

I had an error that I cant reproduce again, but is like a exception where my database tries delete an item:

Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See for information on understanding and handling optimistic concurrency exceptions.
at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ThrowAggregateUpdateConcurrencyException(Int32, Int32, Int32) + 0x7f
at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeResultSetWithoutPropagation(Int32, RelationalDataReader) + 0xbe
at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.Consume(RelationalDataReader) + 0xa7
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection) + 0x174
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable`1, IRelationalConnection) + 0x11b
at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IList`1) + 0x65
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IList`1) + 0x41
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(DbContext, Boolean) + 0x145
at System.Func`3.Invoke(T1, T2) + 0x2f
at Microsoft.EntityFrameworkCore.Storage.Internal.NoopExecutionStrategy.Execute[TState, TResult](TState, Func`3, Func`3) + 0x3d
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean) + 0xf4
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean) + 0x10b
at MyApp.AppRepository.Delete(Person) + 0x46
at MyApp.AppViewModel.<DeleteInvitation>d_262.MoveNext() + 0x17b
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x21
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0x70
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task) + 0x38
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task) + 0x17
at System.Runtime.CompilerServices.TaskAwaiter.GetResult() + 0xb
at MyApp.AppViewModel.<CancelRequestAction>d
_504.MoveNext() + 0xdc

My method:
public void Delete(Person person)

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Based on the error message from the exception, the reason for this is that the item you want to delete has already been deleted or it is modified so the delete command can't find it. Do you have other SQL actions at the same time to this item? That might be the reason for this behavior.

0 Votes 0 ·

I couldn't reproduce this error again, even sending the same command many times.

0 Votes 0 ·
RoyLi-MSFT avatar image RoyLi-MSFT AndersonCavalcante-5944 ·

So this issue happens occasionally. This exception is cached by the Try/Catch block, right? If it is caught, it will not be a problem since you could handle it. If the exception is not caught, that will be a problem.

0 Votes 0 ·
Viorel-1 avatar image Viorel-1 AndersonCavalcante-5944 ·

I couldn't reproduce this error again

Try putting a breakpoint at SaveChanges. When it stops, delete the corresponding row from database manually (using Management Studio, for example). Then continue the program. In another experiment, modify the row instead of deleting.

0 Votes 0 ·

0 Answers