我想自定義Asp.Net Identity 3類。
這是我做的:
public class MyDbContext : IdentityDbContext<User>
和
public class User : IdentityUser<int>
我還擴展了IdentityUserClaim<int>, IdentityRole<int>, IdentityUserLogin<int> and IdentityUserRole<int>
但是我收到以下錯誤:
The type 'User' cannot be used as type parameter 'TUser' in the generic type or method' IdentityDbContext<TUser>'.There is no implicit reference conversion from 'User' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser'.
我不知道你是如何讓你的類繼承IdentityUser<int>
因為`IdentityUser'的通用版本有更多的類型參數。點擊此處: https : //msdn.microsoft.com/en-us/library/dn613256%28v=vs.108%29.aspx
所以,你需要:
public class User : IdentityUser<int, UserLogin, UserRole, UserClaim>
和:
public class UserRole : IdentityUserRole<int> { }
public class UserClaim : IdentityUserClaim<int> { }
public class UserLogin : IdentityUserLogin<int> { }
編輯:對於Identity 3.0,情況有點不同,但問題類似。根據: https : //github.com/aspnet/Identity/blob/dev/src/Microsoft.AspNet.Identity.EntityFramework/IdentityDbContext.cs
IdentityDbContext<TUser>
是這樣的:
public class IdentityDbContext<TUser> : IdentityDbContext<TUser, IdentityRole, string> where TUser : IdentityUser
{ }
重要的部分是where TUser : IdentityUser
IdenityUser的定義是:
public class IdentityUser : IdentityUser<string>
{ ... }
並且您的User
類繼承IdentityUser<int>
因此它沒有隱式轉換為'IdentityUser',因為int
/ string
區別。
解決方案是繼承IdentityDbContext<TUser, TRole, TKey>
,其中TUser將是您的User
類, TRole
將是新的Role類,其中iherits IdentityRole<int>
且TKey
是int
:
public class MyDbContext : IdentityDbContext<User, Role, int> {...}
public class User : IdentityUser<int> {...}
public class Role : IdentityRole<int> {...}
這應該足夠了:
public class ApplicationUser : IdentityUser<int>
{
}
public class ApplicationRole : IdentityRole<int>
{
}
public class MyDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
{
}