将ASP.NET标识用户链接到用户详细信息表

我使用了默认的MVC模板和个人授权。 运行应用程序后,它会自动创建所需的标识表。 我已成功注册了几个用户,所有用户都存储在AspNetUser表中。

现在我想要的是登录用户可以添加更多信息,如名字,姓氏,地址等。为此,我创建了另一个表PersonalInformation其中包含用于存储此信息的列。

我还在EditProfile中创建了一个EditProfile Action。

现在,我如何将ASP.NET Identity用户链接到此表? 我应该将Id列添加为AspNetUser的外键吗? 另外,如何将编辑后的信息与登录的用户ID一起成功存储在“PersonalInformation”表中?

您可以在用户和个人信息之间创建一对一关系,然后使用Application User Manager创建或更新用户。

 public class ApplicationUser : IdentityUser { public async Task GenerateUserIdentityAsync(UserManager 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 virtual PersonalInformation { get; set; } } public class PersonalInformation { [Key, ForeignKey("User")] public string UserId { get;set; } public string FirstName { get; set; } // other fields... public virtual ApplicationUser User { get;set; } } // Create user var store = new UserStore(context); var manager = new ApplicationUserManager(store); var user = new ApplicationUser() { Email = "email@email.com", UserName = "username", PersonalInformation = new PersonalInformation { FirstName = "FirstName" } }; manager.Create(user, "Password123!"); // Update user var store = new UserStore(context); var manager = new ApplicationUserManager(store); var user = manager.Users.FirstOrDefault(u => u.Id == id); user.PersonalInformation.FirstName = "EditedName"; manager.Update(user); 

身份与数据库的接口方式是通过entity framework自动进行的。 在您的解决方案中,应该有一个名为ApplicationDbContext的类,这是entity framework上下文。 我个人建议查找一些entity framework指南,并弄清楚它如何与身份相互作用。 但快速概述将是:

要使用entity framework向数据库添加另一个表,您需要在上下文中添加DbSet。

 public DbSet personalInformation {get;set;} 

然后,您需要更新ApplicationUser以保存对personalInforamtion的引用。 然后,您需要使用生成的迁移或自动迁移将数据库迁移到最新版本。

您随后将进行的任何更改都将通过entity framework进行。

是的,您应该在表中添加一个外键。 如何执行此操作取决于您如何设置entity framework(EDMX /代码优先/从数据库反向设计),但在许多其他问题中解释了此过程。 这是一个单独的问题,之前已经回答过,请尝试搜索。

现在要存储用户的详细信息,您可以在注册此用户时在PersonalInformation表中创建记录,或者在编辑用户时检查它是否存在:

 var currentUserId = User.Identity.GetUserId(); // Look up existing record var personalInformation = _dbContext.PersonalInformation .FirstOrDefault(p => p.UserId == currentUserId); if (personalInformation == null) { // If not exists, create and attach new record personalInformation = _dbContext.PersonalInformation.Create(); } // Map incoming model properties to entity personalInformation.FirstName = model.FirstName; personalInformation.Address = model.Address; // ... // Add or update the entity in the database _dbContext.SaveChanges(); 
 var manager = new UserManager(new UserStore(new ApplicationDbContext())); var store = new UserStore(new ApplicationDbContext()); var ctx = store.Context; var currentUser = manager.FindById(User.Identity.GetUserId()); 
 var manager = new UserManager(new UserStore(new ApplicationDbContext())); var store = new UserStore(new ApplicationDbContext()); var ctx = store.Context; var currentUser = manager.FindById(User.Identity.GetUserId()); pedido.Nombre = currentUser.Nombre;