【发布时间】:2015-03-08 01:48:40
【问题描述】:
这是我第一次在 MVC5 中使用代码优先进行用户管理。我可以更改密码,但无法更新用户注册。我已经梳理了互联网,并逐字查看了该网站上与 UserManager.Update(user) not working 相关的每个问题。
using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
namespace _100..Models
{
public class ApplicationUser : IdentityUser
{
public virtual PersonalInfo PersonalInfo { get; set; }
public virtual BillingInfo BillingInfo { get; set; }
public virtual DeliveryInfo DeliveryInfo { get; set; }
public Chapters Chapter { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class PersonalInfo
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class BillingInfo
{
public int ID { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
public class DeliveryInfo
{
public int ID { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false){}
public DbSet<PersonalInfo> PersonalInfo { get; set; }
public DbSet<BillingInfo> BillingInfo { get; set; }
public DbSet<DeliveryInfo> DeliveryInfo { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
}
这是我的行动
[HttpPost]
public async Task<ActionResult> UpdateRegisteration(ApplicationUser user)
{
var result = await UserManager.UpdateAsync(user);
return RedirectToAction("Index", "Home");
}
我已经确认用户对象在执行操作时具有更新的数据,并且更新方法返回成功,但它实际上并没有更新数据库。
【问题讨论】:
-
您能发布
UserManager声明吗?DbContext这个UserManager使用什么? -
它被传递到我的 AccountController 的构造函数中。 'code' public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager ) { UserManager = userManager;登录管理器 = 登录管理器; }
-
它是在哪里创建的?
-
这是来自 VS 'code' 创建的 app_start/IdentityConfig.cs 类 var manager = new ApplicationUserManager(new UserStore
(context.Get ())); -
我的 AccountController 中的所有内容都使用相同的 UserManager,但是当调用 Update 方法时,它实际上根本不会访问数据库。我分析了它。
标签: c# entity-framework asp.net-mvc-5 owin