How to update IdentityUser with custom properties using MVC5 and entity framework

后端 未结 2 1350
心在旅途
心在旅途 2021-02-01 20:09

I am using the built in identity framework for user management, and would like to add a few customizations to the AspNetUsers table. So far the solution to each problem I\'ve en

2条回答
  •  别那么骄傲
    2021-02-01 20:15

    I faced the same problem. An easy way to overcome this was just to take the properties I wanted to update from the model and save them back to an object pulled from the UserManager;

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ApplicationUser model)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser u = UserManager.FindById(model.Id);
                u.UserName = model.Email;
                u.Email = model.Email;
                u.StaffName = model.StaffName; // Extra Property
                UserManager.Update(u);
                return RedirectToAction("Index");
            }
            return View(model);
        }
    

提交回复
热议问题