C# Delete Operation - DbUpdateConcurrencyException

Anderson Cavalcante 21 Reputation points
2022-09-14T00:34:28.997+00:00

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 http://go.microsoft.com/fwlink/?LinkId=527962 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(IEnumerable1, IRelationalConnection) + 0x11b at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IList1) + 0x65
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IList1) + 0x41 at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(DbContext, Boolean) + 0x145 at System.Func3.Invoke(T1, T2) + 0x2f
at Microsoft.EntityFrameworkCore.Storage.Internal.NoopExecutionStrategy.ExecuteTState, TResult + 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)
{
try
{
_dbContext.Wait();
this._dbContext.Person.Remove(person);
this._dbContext.SaveChanges();
}
finally
{
_dbContext.Release();
}
}

Universal Windows Platform (UWP)
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,704 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,238 questions
{count} votes