You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

EF Core RowVersion并发检查未捕获DbConcurrencyError问题求助

EF Core并发冲突检测失效问题

我使用EF Core和React开发了一个项目,需求是当两个用户同时打开同一页面并先后修改同一条数据时,给后修改的用户抛出“已被提前修改”的错误。但按照配置实现后,系统未捕获任何错误,数据库仍完成更新且生成了新的RowVersion。

实现配置

ModelCreating配置

modelBuilder.Entity<User>().Property(e => e.RowVersion)
    .IsRequired()
    .IsRowVersion()
    .IsConcurrencyToken();

实体类定义

public class User : AbpUser<User>
{
    [Timestamp]
    public byte[] RowVersion { get; set; }
}

预期触发错误的测试代码

public override async Task<UserDto> UpdateAsync(UserDto input) {

    CheckUpdatePermission();

    //input.RowVersion = new byte[] { 0, 0, 0, 0, 0, 0, 7, 215 };

    var user = await _userManager.GetUserByIdAsync(input.Id);

    //MapToEntity(input, user);

    //user.UserName = "hh";

    //user.RowVersion = new byte[] { 1, 2, 0, 0, 0, 0, 9, 85};
    user.RowVersion[1] =55;

    try
    {
        CheckErrors(await _userManager.UpdateAsync(user));
    }
    catch (Exception e)
    {
        throw new UserFriendlyException(e.Message);
    }

    if (input.RoleNames != null)
    {
        CheckErrors(await _userManager.SetRolesAsync(user, input.RoleNames));
    }

    return await GetAsync(input);
}

相关截图说明

  • 控制台应用实现示例截图
  • DbContext配置截图
  • AppService实现截图
  • 实体类定义截图
  • RowVersion数据库列截图

内容的提问来源于stack exchange,提问作者Sahin Kenanoglu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 02:05:58