2015-10-18 127 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

相關問題