有沒有辦法在IdentityUser
,IdentityRole
和IdentityDbContext
中指定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();
}
}
我創建了一個自定義角色'ApplicationRole',你必須在指定它'MyAppDb:IdentityDbContext <...>'。 –
所以我嘗試了你的建議,但必須像這樣創建'MyAppDb':'public class GEOdaisiaMembershipDb:IdentityDbContext'並且得到了這個錯誤'實體類型IdentityRole不是當前上下文的模型.'在這行代碼中'var identity = await UserManager.CreateIdentityAsync(user,DefaultAuthenticationTypes.ApplicationCookie);'註冊用戶時。 –
看看這個[thread](http://stackoverflow.com/questions/19865245/how-to-extend-microsoft-aspnet-identity-entityframework-identityrole) – dima