C# 引发异常:';Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException';在System.Private.CoreLib.ni.dll中

C# 引发异常:';Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException';在System.Private.CoreLib.ni.dll中,c#,visual-studio,api,postman,C#,Visual Studio,Api,Postman,我创建了此包分配方法: [HttpPut("api/auth/user/{id}")] public async Task<IActionResult> AssignPackage(string id ,[FromBody] AppUser user) { try { var newUser = Mapper.Map<AppUser, UserViewModel>(user);

我创建了此包分配方法:

 [HttpPut("api/auth/user/{id}")]
    public async Task<IActionResult> AssignPackage(string id ,[FromBody] AppUser user)
    {
        try
        {
            var newUser = Mapper.Map<AppUser, UserViewModel>(user);

            var userToEdit = await _context.AppUser
            .AsNoTracking()
            .SingleOrDefaultAsync(m => m.Id == id);

            newUser.PackageType = user.AccountType;

            if (userToEdit == null)
            {
                return NotFound("Could not update user as it was not found");
            }
            else
            {
                _context.Update(user);
                await _context.SaveChangesAsync();
                //return Ok(newUser);
            }


            UserViewModel _userVM =

              Mapper.Map<AppUser, UserViewModel>(user);

            return new OkObjectResult(_userVM);
        }
        catch (DbUpdateException)
        {
            //Log the error (uncomment ex variable name and write a log.)
            ModelState.AddModelError("", "Unable to save changes. " +
                "Try again, and if the problem persists, " +
                "see your system administrator.");
            return NotFound("User not Found");
        }


    }

鉴于
AppUser
继承自
IdentityUser
,调用
\u context.Update(user)时没有
Id

我怀疑如果你检查这个字段,它会是0,因为它是身体的一部分


由于此原因,EF无法找到要更新的实体,这就是为什么您会得到异常的原因,因为
AppUser
继承自
IdentityUser
,当您调用
\u context.update(user)没有
Id

我怀疑如果你检查这个字段,它会是0,因为它是身体的一部分


由于此原因,EF找不到要更新的实体,这就是为什么会出现异常

尝试向catch添加一个变量,即
catch(DbUpdateExecption ex)
然后检查该值以查看异常详细信息
ex{Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException:数据库操作预期影响1行,但实际影响0行。自加载实体后,数据可能已被修改或删除。位于Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ThrowagGateUpdateConcurrencException(Int32 commandIndex、Int32 ExpectedRowsInfected、Int32 rowsAffected)
在Catch异常中中断指向新的“ex”变量时,我会遇到类似这样的错误。鉴于
AppUser
继承自
IdentityUser
,调用
\u context.Update(用户)时;
没有
Id
。我怀疑如果您检查该字段,它将是0,因为它是作为正文的一部分进入的。EF找不到要更新的实体,这就是为什么您会出现异常。当我再次中断代码以双重检查时,所有实例中的Id值都是作为我通过的Id返回的然而,对于postman,还有其他值(如用户名等)仍然为空,这可能是因为吗?不太可能。异常表明数据库中不再存在数据,或者在调用方法时找不到数据。是否尝试设置
usertoEdit
的值并保存?尝试添加变量b请转到catch,即
catch(DbUpdateExecption ex)
,然后检查该值以查看异常详细信息
ex{Microsoft.EntityFrameworkCore.dbupdatecurrencyException:数据库操作预期影响1行,但实际影响0行.Data可能在加载实体后已被修改或删除。在Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ThrowAggregateUpdateConcurrencyException(Int32 commandIndex、Int32 ExpectedRowsInfected、Int32 rowsAffected)
当在Catch异常中中断指向新的“ex”变量时,我会遇到类似这样的错误。鉴于
AppUser
继承自
IdentityUser
似乎在调用
\u context.Update(user)时;
没有
Id
。我怀疑如果您检查该字段,它将是0,因为它是作为正文的一部分进入的。EF找不到要更新的实体,这就是为什么您会出现异常。当我再次中断代码以双重检查时,所有实例中的Id值都是作为我通过的Id返回的然而,对于postman,还有其他值(如用户名等)仍然为空,这可能是因为吗?不太可能。异常表明数据库中不再存在数据,或者在调用方法时找不到数据。您是否尝试设置
usertoEdit
的值并保存?
 public class AppUser : IdentityUser 
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string AccountType { get; set; }
        public int? PeriodOrderQty { get; set; }
        public int? TotalOrderQty { get; set; }
        public Guid? APIKey { get; set; }
        public Packages Package { get; set; }
        public Cities City { get; set; }
        public QualificationLevels Qualifications { get; set; }
        public string Token { get; set; }

    }

public class UserViewModel
    {
        public string Id { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        public string PackageType { get; set; }
        public int? PeriodOrderQty { get; set; }
        public int? TotalOrderQty { get; set; }
        public Guid? APIKey { get; set; }
    }