简体   繁体   中英

IdentityUser.Update(user) another entity of the same type already has the same primary key value


    [HttpPost]
    public ActionResult Edit(ApplicationUser user, string password)
    {  
        if(password.IsEmpty() == false)
        {
            user.PasswordHash = userManager.PasswordHasher.HashPassword(password);
        }
        user.Status = Enums.DataStatus.Updated;


        var result =userManager.Update(user);  //error!! 


        return RedirectToAction("EmployeeList");
    }

If I change the UserName variable while posting the edit, I don't get an error, but when I use it without changing it, it gives an error. How can I fix it (I want to use it by changing other properties without changing the username)

You must attached the User to an existing user first.

    [HttpPost]
    public ActionResult Edit(ApplicationUser user, string password)
    {  

        var Existinguser = await UserManager.FindByNameAsync(user.userName);

        if(password.IsEmpty() == false)
        {
            Existinguser.PasswordHash = userManager.PasswordHasher.HashPassword(password);
        }
        Existinguser.Status = Enums.DataStatus.Updated;

        //...

        //Update the existing user here
        // ExistingUser.Address = user.Address;
        //..

        var result =userManager.Update(ExistingUser);  //Update existing user here


        return RedirectToAction("EmployeeList");
    }

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