2014-11-22 58 views
2

我是ASP.Net身份模型的新手,並試圖創建我的ApplicationUser類的子類。我想將它們保存在不同的表在我的數據庫:子類沒有保存在數據庫中的ASP.Net MVC標識模型

[Table("Persons")] 
public class Person : ApplicationUser{...} 

我創造的人類這樣在我的種子方法:

Person testUser = new Person(); 
UserManager.Create(testUser); 

但這隻有在ApplicationUser表中創建條目我數據庫。創建它們之後,我嘗試使用用戶標識獲取人員數據:

var userID = User.Identity.GetUserId() 

using (ApplicationDbContext context = new ApplicationDbContext()) 
{ 
    var result = context.PersonModels.FirstOrDefault(x => x.Id == userID); 
} 

但是表格總是空的。有沒有辦法讓這個工作?

+0

是什麼UserManager.Create做,顯示代碼中的註釋。 – Jaanus 2014-11-22 13:57:34

+0

@Jaanus'UserManager'由OP所擁有的MVC框架(幾乎可以肯定)不提供這種方法。 – 2014-11-22 22:25:35

回答

2

我懷疑UserManager.Create()期待一個ApplicationUser對象,而不是Person並且,正因爲如此,只是把它當作一個ApplicationUser

您應該將您的應用程序特定屬性添加到ApplicationUser類,而不是繼承它。描述過程的指南是here

在你只需要在/Models/identity.cs文件中添加這個類定義的屬性概括地說:

public class ApplicationUser : IdentityUser 
    { 

然後做necesary遷移騷動。

其實,我的MVC的版本很有幫助包含一個鏈接到Identity.cs因人而異是非常教程:-)

相關問題