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

后端 未结 2 1351
心在旅途
心在旅途 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);
        }
    
    0 讨论(0)
  • 2021-02-01 20:36

    So, you are using database-first instead of code-first. My recommendation is to use code-first. I, like you, started out using database first, instead. If you aren't using code-first, then delete the migrations table in your database. Otherwise, it will cause problems.

    If possible, I recommend following this code-first tutorial, which has helped me greatly. You can easily add custom fields to your identity and have full integration with the framework as far as authentication goes.

    http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx

    0 讨论(0)
提交回复
热议问题