简体   繁体   中英

Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException' in System.Private.CoreLib.ni.dll

I created this package assign method:

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


    }

The aim of the method is that I input the Account type in postman and the output should be the user view model with an updated Package Type. However when inputting the accountType into postman the output is 'User not found' and the error is '404 Not found' .

In Visual Studio the error being shown is the title of the question. unsure what the problem may be (only beginner level experience).

The models being used:

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

Given that AppUser inherits from IdentityUser it seems that when you call _context.Update(user); there is no Id .

I suspect that if you check that field it will be 0, as it's coming in as part of the body.

EF can't find an entity to update because of this, and that's why you're getting the exception

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM