Home > Back-end >  Why am I getting this error when trying to delete a single object?
Why am I getting this error when trying to delete a single object?

Time:05-06

I am getting this error when a user deletes a record on my Entity Framework Core website:

Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: The database operation was expected to affect 1 row(s), but actually affected 0 row(s);

This line is where the error occurs: await _context.SaveChangesAsync();

I setup my delete controller following this page: https://docs.microsoft.com/en-us/ef/core/saving/basic

Here is the code:

public class GameWorldsController : ControllerBase
{
    private readonly actionGames_ProdContext _context;
    
    public GameWorldsController(actionGames_ProdContext context)
    {   
        _context = context;
        
    }
    
    [HttpDelete("{id}")]    
    public async Task<ActionResult<GameWorlds>> DeleteGameWorlds(string id)
    {
        var gameWorlds = await _context.GameWorlds.FindAsync(id);
        
        if (gameWorlds == null)
        {
            return NotFound("Game World does not exist.");
        }
        
        _context.GameWorlds.Remove(gameWorlds);
        await _context.SaveChangesAsync();
        
        return gameWorlds;
    }
}

Is there anything I can do to fix this?

The full error states:

 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.

   at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ThrowAggregateUpdateConcurrencyException(Int32 commandIndex, Int32 expectedRowsAffected, Int32 rowsAffected)

   at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeResultSetWithoutPropagationAsync(Int32 commandIndex, RelationalDataReader reader, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeAsync(RelationalDataReader reader, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IList`1 entriesToSave, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(StateManager stateManager, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)

CodePudding user response:

It looks like a race condition, where EF try to delete the record when it has been already deleted before SaveChangesAsync. You can try this to verify if it is the case:

  1. Put breakpoint at SaveChangesAsync.
  2. Start debugging.
  3. Invoke the API and wait until the debugger hits the breakpoint.
  4. Delete the record from the database
  5. Continue debugging.

If it is the case, you will see the same error. There is no quick fix for that, you might try to catch that specific exception and ignore it if you want.

  • Related