2

有沒有辦法在IdentityUser,IdentityRoleIdentityDbContext中指定TKey string來創建自定義用戶和角色?我問,因爲它似乎認爲我不想自動生成主鍵Id了,我絕對會這樣做。按照我在下面所做的操作,UserManager.Create(user, password)將在Id上失敗,並顯示EntityValidationError有沒有在IdenitityUser,IdentityRole和IdentityDbContext上指定TKey的情況下創建自定義用戶和角色的方法?

public class ApplicationUser : IdentityUser<string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim> 
{ 
    [Required] 
    [StringLength(50)] 
    public string FirstName { get; set; } 

    [Required] 
    [StringLength(50)] 
    public string LastName { get; set; } 
} 

public class ApplicationUserLogin : IdentityUserLogin 
{ 
} 

public class ApplicationUserClaim : IdentityUserClaim 
{ 
} 

public class ApplicationUserRole : IdentityUserRole 
{ 
} 

public class ApplicationRole : IdentityRole<string, ApplicationUserRole> 
{ 
    [Required] 
    [StringLength(50)] 
    public string ProperName { get; set; } 

    [Required] 
    public string Description { get; set; } 
} 


public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim> 
{ 
    public MyAppDb() 
     : base("MyAppDb") 
    { 
    } 

    public static MyAppDb Create() 
    { 
     return new MyAppDb(); 
    } 
} 

回答

0

看起來答案是「NO」。如果您在User和/或Role上指定了TKey,則不再爲您創建主鍵。

看來我試圖讓事情過度複雜化。感謝@dima幫助我決定不復雜的事情。這是我做過什麼成功獲得用戶和角色(可以使用自定義屬性)順利進行,即通過一個控制器和視圖,以成功創建數據庫中的記錄:

UPDATE:您可能想看看我在底部提供了一個更好/更簡單的解決方案。

public class ApplicationUser : IdentityUser 
{ 
    [Required] 
    [StringLength(50)] 
    public string FirstName { get; set; } 

    [Required] 
    [StringLength(50)] 
    public string LastName { get; set; } 
} 

//public class ApplicationUserLogin : IdentityUserLogin 
//{ 
//} 

//public class ApplicationUserClaim : IdentityUserClaim 
//{ 
//} 

//public class ApplicationUserRole : IdentityUserRole 
//{ 
//} 

public class ApplicationRole : IdentityRole 
{ 
    [Required] 
    [StringLength(50)] 
    public string ProperName { get; set; } 
} 


public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim> 
{ 
    public MyAppDb() 
     : base("MyAppDb") 
    { 
    } 
} 

但是,一個新的問題產生了UserManager。具體來說,我在這行代碼var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);收到錯誤The entity type IdentityRole is not part of the model for the current context.

UPDATE:修正了這個錯誤在這裏:Why am I getting an IdentityRole error?

0

我還在學習這個新的ASP.NET識別系統,但你試過省略泛型類型是這樣的:

public class ApplicationUser : IdentityUser 
{ 
    [Required] 
    [StringLength(50)] 
    public string FirstName { get; set; } 

    [Required] 
    [StringLength(50)] 
    public string LastName { get; set; } 
} 

同樣適用於ApplicationRole

MyAppDb是這樣的:

public class MyAppDb: IdentityDbContext<ApplicationUser> 

默認情況下,ID將是字符串與自動生成的DB中的GUID

+0

我創建了一個自定義角色'ApplicationRole',你必須在指定它'MyAppDb:IdentityDbContext <...>'。 –

+0

所以我嘗試了你的建議,但必須像這樣創建'MyAppDb':'public class GEOdaisiaMembershipDb:IdentityDbContext '並且得到了這個錯誤'實體類型IdentityRole不是當前上下文的模型.'在這行代碼中'var identity = await UserManager.CreateIdentityAsync(user,DefaultAuthenticationTypes.ApplicationCookie);'註冊用戶時。 –

+0

看看這個[thread](http://stackoverflow.com/questions/19865245/how-to-extend-microsoft-aspnet-identity-entityframework-identityrole) – dima

相關問題