Using .NET6 and EF Core I'm trying to update a user's details in the database and receiving an error:
DbUpdateConcurrencyException: The database operation was expected to affect 1 row(s), but actually affected 0 row(s);
I'm using a class called AppUser
which inherits from IdentityUser
.
My update method looks like this:
public async Task<IActionResult> UpdateAccountAsync(AppUser user)
{
if (!ModelState.IsValid) return View(nameof(MyAccount), user);
user.ModifiedDateT = DateTime.UtcNow;
await _userRepository.UpdateAsync(user);
return View(nameof(MyAccount), user);
}
With the UpdateAsync
method like:
public async Task<AppUser> UpdateAsync(AppUser entity)
{
if (entity == null)
throw new InvalidOperationException("Cannot update a null object!");
entity.ModifiedDateT = DateTime.UtcNow;
var dbEntity = _context.Update(entity);
await _context.SaveChangesAsync();
return dbEntity.Entity;
}
Using this interface:
Task<AppUser> UpdateAsync(AppUser entity);
With my limited understanding of EF Core, do you have to bring in the AppUser
, make changes to the AppUser
, then update and save? It's not working out for me