c# - 身份用户管理器 DeleteAsync DbUpdateConcurrencyException

标签 c# asp.net-core entity-framework-core asp.net-core-identity

我正在尝试通过 webapi 后面的 aspnetcore.identity UserManager 删除用户。

    [HttpPost("Delete", Name = "DeleteRoute")]
    [Authorize(Roles = "SuperUser")]
    public async Task<IActionResult> DeleteAsync([FromBody] User user)
    {
        Console.WriteLine("Deleting user: " + user.Id);
        try {
            await _userManager.DeleteAsync(user);
            return Ok();
        } catch(Exception e) {
            return BadRequest(e.Message);
        }

    }

这会抛出一个 DbUpdateConcurrencyException

   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 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.Internal.BatchExecutor.ExecuteAsync(DbContext _, ValueTuple`2 parameters, 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.ChangeTracking.Internal.StateManager.SaveChangesAsync(IReadOnlyList`1 entriesToSave, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
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 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.Internal.BatchExecutor.ExecuteAsync(DbContext _, ValueTuple`2 parameters, 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.ChangeTracking.Internal.StateManager.SaveChangesAsync(IReadOnlyList`1 entriesToSave, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)

我知道这个异常通常表示竞争条件,但我不明白为什么会这样。

我做错了什么吗?

编辑

我发布的用户对象看起来像这样:

"User": {
 "Email": "",
 "FirstName": "",
 "LastName": "",
 "Gender": "",
 "Affiliation": {
     "isStudent": true,
     "isEmployee": false
   }
   ...
}

最佳答案

Entity Framework Core 使用 Optimistic Concurrency :

In an optimistic concurrency model, a violation is considered to have occurred if, after a user receives a value from the database, another user modifies the value before the first user has attempted to modify it.

将此与悲观并发进行对比:

...in a pessimistic concurrency model, a user who updates a row establishes a lock. Until the user has finished the update and released the lock, no one else can change that row.

为了实现开放式并发,IdentityUser 类包含一个 ConcurrencyStamp属性(以及数据库中的相应列),它是 GUID 的字符串表示形式:

public virtual string ConcurrencyStamp { get; set; } = Guid.NewGuid().ToString();

每次将用户保存到数据库时,ConcurrencyStamp 都会设置为新的 GUID。

以删除用户为例,发送到服务器的 DELETE SQL 语句的简化版本可能如下所示:

DELETE FROM dbo.AspNetUsers
WHERE Id = '<USER_ID>' AND ConcurrencyStamp = '<CONCURRENCY_STAMP>'

当上述 SQL 语句中的 CONCURRENCY_STAMP 值与给定用户在数据库中存储的值不匹配时,会出现您收到的错误消息。这确保如果您从数据库(包含特定的 ConcurrencyStamp)检索用户,则只有在其他地方没有进行其他更改(因为您提供相同的)时,您才能将更改保存到数据库ConcurrencyStamp 数据库中存在的值)。

正如您从上面的 ConcurrencyStamp 定义中看到的,该属性默认为一个新的 GUID - 每次 IdentityUser(或子类)创建后,它会获得一个新的 ConcurrencyStamp 值。在您的示例中,通过传递给您的 DeleteAsync 操作的 User,ASP.NET Core 模型绑定(bind)首先创建一个新的 User 实例> 然后设置 JSON 负载中存在的属性。由于负载中没有 ConcurrencyStamp 值,User 将以一个 ConcurrencyStamp 值结束与数据库中的匹配。

为避免此问题,您可以ConcurrencyStamp 值添加到从客户端发送的负载中。但是,我不会推荐这个。解决此问题的最简单和最安全的方法是将 UserId 作为有效负载发送,使用 检索 User 本身>_userManager.FindByIdAsync,然后使用那个实例来执行删除。这是一个例子:

[HttpPost("Delete/{id}", Name = "DeleteRoute")]
[Authorize(Roles = "SuperUser")]
public async Task<IActionResult> DeleteAsync(string id)
{
    Console.WriteLine("Deleting user: " + id);
    try {
        var user = await _userManager.FindByIdAsync(id);

        if(user == null)
            // ... 

        await _userManager.DeleteAsync(user);
        return Ok();
    } catch(Exception e) {
        return BadRequest(e.Message);
    }
}

关于c# - 身份用户管理器 DeleteAsync DbUpdateConcurrencyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52052147/

相关文章:

c# - 从运行时分析生成测试

C# 窗体句柄属性

c# - 如何更改文本 block 中特定单词的前景?

asp.net-core - .NET Core 数据库模型在生产中的变化

c# - 我可以使用 for 循环等待网络浏览器完成导航吗?

c# - 为什么在 .net core 3.1 中找不到 Web Api 授权服务?

c# - 如何使用 AutoMapper 从多个对象映射单个对象?

asp.net-core - asp.net Core Identity 中的 Multiple & SubDomain cookie

.net - Asp.net core mvc 批准后更新

c# - EF : manual (non autogenerated) keys require ad hoc handling of new entities