2015-10-18 52 views
4

我使用了具有单独授权的默认MVC模板。运行应用程序后,它会自动创建所需的标识表。我已经成功注册了一些用户,并且他们都存储在AspNetUser表中。将ASP.NET身份用户链接到用户详细信息表

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

我也在ManageController中创建了EditProfile动作。

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

回答

5

您可以创建一个到:

现在来存储信息的用户,你可以在你的PersonalInformation表在注册该用户,或检查,如果它在编辑用户时存在创造了纪录用户和个人信息之间的一种关系,然后使用应用程序用户管理器创建或更新用户。

public class ApplicationUser : IdentityUser 
{ 
    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 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<ApplicationUser>(context); 
var manager = new ApplicationUserManager(store); 
var user = new ApplicationUser() { Email = "[email protected]", UserName = "username", PersonalInformation = new PersonalInformation { FirstName = "FirstName" } }; 
manager.Create(user, "Password123!"); 

// Update user 
var store = new UserStore<ApplicationUser>(context); 
var manager = new ApplicationUserManager(store); 
var user = manager.Users.FirstOrDefault(u => u.Id == id); 
user.PersonalInformation.FirstName = "EditedName"; 
manager.Update(user); 
+0

你好,我也是新的MVC。我正在尝试做类似于上述问题的事情。我创建了我的链接表,现在我试图使用类似于您的示例的代码来存储数据,但无法确定放置位置。我在哪里输入此代码来执行此操作? –

+0

这应该是正确的答案 – Samra

+0

埃丁在我的情况下,我想先添加个人信息,然后在个人信息中的i​​d应该成为用户身份证号码 – Samra

0

身份与数据库接口的方式是通过实体框架自动实现的。在你的解决方案中应该有一个名为ApplicationDbContext的类,这是实体框架上下文。就个人而言,我会建议查找一些实体框架指南,并研究它如何与身份进行交互。但快速概述如下:

要使用实体框架将另一个表添加到数据库中,您需要在上下文中添加一个DbSet。

public DbSet<PersonalInformation> personalInformation {get;set;} 

那么你就需要更新ApplicationUser举行的personalInforamtion参考。 然后,您需要将数据库迁移到最新版本,无论是生成的迁移还是自动迁移。

然后您将做的任何更改都将通过实体框架。

0

是的,你应该添加一个外键到你的表。如何做到这一点取决于你如何设置你的实体框架(EDMX/code first/reverse engineered from database),但是这个过程在很多其他问题中都有解释。这是一个单独的问题,之前已经回答,请尝试搜索。

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(); 
0
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())); 
       var store = new UserStore<ApplicationUser>(new ApplicationDbContext()); 
       var ctx = store.Context; 
       var currentUser = manager.FindById(User.Identity.GetUserId()); 
-1
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())); 
var store = new UserStore<ApplicationUser>(new ApplicationDbContext()); 
var ctx = store.Context; 
var currentUser = manager.FindById(User.Identity.GetUserId()); 
pedido.Nombre = currentUser.Nombre; 
+0

请添加一些解释上述代码如何解决问题。 – ekad

+0

你不明白什么?我作为aspNetUser领域的参考值班 – Max