为什么在尝试删除单个对象时会出现此错误?
Why am I getting this error when trying to delete a single object?
当用户删除我的 Entity Framework 核心网站上的记录时出现此错误:
Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: The
database operation was expected to affect 1 row(s), but actually
affected 0 row(s);
这一行是错误发生的地方:await _context.SaveChangesAsync();
我按照此页面设置删除控制器:https://docs.microsoft.com/en-us/ef/core/saving/basic
代码如下:
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;
}
}
我能做些什么来解决这个问题吗?
完整的错误状态:
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)
这看起来像是一个竞争条件,其中 EF 尝试删除在 SaveChangesAsync
之前已经删除的记录。你可以试试这个来验证是否是这样:
- 将断点放在
SaveChangesAsync
。
- 开始调试。
- 调用 API 并等待调试器到达断点。
- 从数据库中删除记录
- 继续调试。
如果是这样,您将看到相同的错误。对此没有快速解决方法,您可以尝试捕获该特定异常并根据需要忽略它。
当用户删除我的 Entity Framework 核心网站上的记录时出现此错误:
Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: The database operation was expected to affect 1 row(s), but actually affected 0 row(s);
这一行是错误发生的地方:await _context.SaveChangesAsync();
我按照此页面设置删除控制器:https://docs.microsoft.com/en-us/ef/core/saving/basic
代码如下:
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;
}
}
我能做些什么来解决这个问题吗?
完整的错误状态:
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)
这看起来像是一个竞争条件,其中 EF 尝试删除在 SaveChangesAsync
之前已经删除的记录。你可以试试这个来验证是否是这样:
- 将断点放在
SaveChangesAsync
。 - 开始调试。
- 调用 API 并等待调试器到达断点。
- 从数据库中删除记录
- 继续调试。
如果是这样,您将看到相同的错误。对此没有快速解决方法,您可以尝试捕获该特定异常并根据需要忽略它。