Visual Studio 抛出的异常:System.Private.CoreLib.ni.dll中的“Microsoft. MicrosoftFrameworkCore.DbUpdateConcurrencyException”

brccelvz  于 2023-10-23  发布在  其他
关注(0)|答案(2)|浏览(105)

我创建了这个包分配方法:

[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");
        }
     
       
    }

该方法的目的是,我在postman中输入Account类型,输出应该是具有更新的PackageType的用户视图模型。但是,当将accountType输入postman时,输出为'User not found',错误为'404 Not found'
在Visual Studio中,显示的错误是问题的标题。不确定问题可能是什么(只有初学者水平的经验)。
使用的模型:

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; }
    }
vwhgwdsa

vwhgwdsa1#

假设AppUser继承自IdentityUser,那么当你调用_context.Update(user);时,似乎就没有Id了。
我怀疑,如果你检查该字段,它将是0,因为它是作为身体的一部分进来的。
EF找不到要更新的实体,这就是为什么你会得到异常

x759pob2

x759pob22#

我在SQLite中遇到了这个错误。我怀疑这是因为更改跟踪器由于某种原因无法正确处理SQLite提供程序中的GUID ID。我从更改跟踪器(SaveChanges)切换到ExecuteUpdate,它工作正常。我是这么做的:

var affectedRows = _context.Companies
    .Where(x => ids.ToList().Contains(x.Id.ToString()))
    .ExecuteUpdate(setters => setters.SetProperty(b => b.IsActive, false));

您可以查看“EF Core ExecuteUpdate文档”以获取更多示例。

相关问题