I have a method that inserts several data from an excel spreadsheet into the database, but there is some data, such as email, password, or concurrencyStamp (set to NULL), so I can not change user data by means of the code.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> RegisterUserCreated(ApplicationUser user)
{
if (ModelState.IsValid)
{
user.UserName = user.Email;
user.NormalizedUserName = user.Email.ToTitleCase();
user.NormalizedEmail = user.Email.ToTitleCase();
if (user.Id_Matricula != null) user.Id = (int) user.Id_Matricula;
user.Departamento = _context.TbDepartamentos.Find(user.Id_Departamento);
var password = new PasswordHasher<ApplicationUser>();
var passwordHash = password.HashPassword(user, "Senha");
user.PasswordHash = passwordHash;
user.ConcurrencyStamp = await _userManager.GenerateConcurrencyStampAsync(user);
await _userManager.UpdateSecurityStampAsync(user);
_context.ApplicationUsers.Update(user);
_context.SaveChanges();
}
}
Error
An unhandled exception occurred while processing the request.
DbUpdateConcurrencyException: Database operation expected to affect 1 row (s) but actually affected 0 row (s). Data may have been modified or deleted since characters were loaded. See link for information on understanding and handling optimistic concurrency exceptions. Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ThrowAggregateUpdateConcurrencyException (int commandIndex, int expectedRowsAffected, int rowsAffected)