更新用户数据 - ASP.NET身份 [英] Updating user data - ASP.NET Identity
问题描述
我已经加入到 ApplicationUser
类结果的自定义字段
我还创建,通过它用户可以输入/编辑字段的表格。结果
但是由于某种原因,我不能在数据库中更新的字段。
I've added custom fields to the ApplicationUser
class
I've also created a form through which the user can enter/edit the fields.
However for some reason I'm not able to update the fields in the database.
[HttpPost]
[ActionName("Edit")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Manage(EditProfileViewModel model)
{
if (ModelState.IsValid)
{
// Get the current application user
var user = User.Identity.GetApplicationUser();
// Update the details
user.Name = new Name { First = model.FirstName, Last = model.LastName, Nickname = model.NickName };
user.Birthday = model.Birthdate;
// This is the part that doesn't work
var result = await UserManager.UpdateAsync(user);
// However, it always succeeds inspite of not updating the database
if (!result.Succeeded)
{
AddErrors(result);
}
}
return RedirectToAction("Manage");
}
我的问题是类似 MVC5 ApplicationUser自定义属性的,但似乎使用旧因为IdentityManager类似乎认同的版本不存在。
My problem is similar to MVC5 ApplicationUser custom properties, but that seems to use an older version of Identity because the IdentityManager class doesn't seem to exist.
有人能指导我如何更新数据库中的用户
信息?
Can someone guide me on how to update User
info in the database?
更新:
如果我包含在登记表中的所有字段,所有的值存储在相应的字段中的用户的新纪录
表从数据库中。
我不知道要更改现有用户(行中的用户
表)的领域。 UserManager.UpdateAsync(用户)
不起作用。
I don't know to make changes to the fields of an existing user (row in the users
table). UserManager.UpdateAsync(user)
doesn't work.
的另外请注意我的问题是面向比的EntityFramework更多身份的
推荐答案
OK ......我花了几个小时试图找出为什么 userManager.updateAsync
将不会保留用户数据我们编辑...直到我得出了以下的结论:
OK... I spent hours trying to figure why userManager.updateAsync
would not persist the user data that we edit ... until I reached the following conclusion:
混乱源于我们创建的UserManager
在这样一条线的事实:
The confusion arises from the fact that we create the UserManager
in one line like this:
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new MyDbContext()));
......然后我们用 manager.UpdateAsync(用户);
,但将更新在上下文中的用户,然后我们需要将更改保存到的DbContext的身份。所以,问题是如何获得认同的DbContext的最简单的方法。
...then we use manager.UpdateAsync( user );
but that will update the user in the context, and then we will need to save changes to the dbcontext of the Identity. So, the question is how to get the Identity DBcontext in the easiest way.
要解决这个问题,我们不应该在一行创建的UserManager
...这里是我如何做到这一点:
To solve this, we should not create the UserManager
in one line ... and here is how I do it:
var store = new UserStore<ApplicationUser>(new MyDbContext());
var manager = new UserManager(store);
然后通过调用更新用户后
then after updating the user by calling
manager.UpdateAsync(user);
然后你去到上下文
then you go to the context
var ctx = store.context;
然后
ctx.saveChanges();
wahooooooo ......坚持:)
wahooooooo...persisted :)
希望这会帮助别人谁拉着自己的头发了几个小时:P
Hope this will help someone who pulled their hair for a few hours :P
这篇关于更新用户数据 - ASP.NET身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!