0

Message:

Database operation expected to affect 1 row(s) but actually affected 0 row(s).

I am trying to update user info in the database but I get this error.

public IActionResult OnPost()
{
    if (!string.IsNullOrEmpty(user.Password))
    {
        PasswordHasher<User> Hasher = new PasswordHasher<User>();
        user.Password = Hasher.HashPassword(user, user.Password);
    }
    else
    {
        //user.Password = _mycontext.Users
        //    .SingleOrDefault(a => a.Id == user.Id).Password;
    }
    
    _mycontext.Entry(user).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
    _mycontext.SaveChanges();
    return Page();
}
2
  • Does user have a correct primary key value? Commented May 31, 2021 at 7:03
  • Yeah, never mind, solved it. I didn't know the id changes for the same user, so I had to fetch it manually from the database.
    – Ash
    Commented May 31, 2021 at 7:08

0