簡體   English   中英

為什么在嘗試刪除單個 object 時出現此錯誤?

[英]Why am I getting this error when trying to delete a single object?

當用戶刪除我的 Entity Framework Core 網站上的記錄時,我收到此錯誤:

Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException:數據庫操作預計會影響 1 行,但實際上影響了 0 行;

此行是發生錯誤的地方: await _context.SaveChangesAsync();

我在這個頁面之后設置了我的刪除 controller: https://learn.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之前已被刪除的記錄。 您可以試試這個來驗證是否是這種情況:

  1. SaveChangesAsync處放置斷點。
  2. 開始調試。
  3. 調用 API 並等待調試器到達斷點。
  4. 從數據庫中刪除記錄
  5. 繼續調試。

如果是這種情況,您將看到相同的錯誤。 沒有快速解決方法,您可以嘗試捕獲該特定異常並根據需要忽略它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM