我想自定义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'.
答案 0 :(得分:4)
我不知道你是如何让你的类继承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
将是新的角色类,其中iherits IdentityRole<int>
和TKey
是int
:
public class MyDbContext : IdentityDbContext<User, Role, int> {...}
public class User : IdentityUser<int> {...}
public class Role : IdentityRole<int> {...}
答案 1 :(得分:3)
这应该足够了:
public class ApplicationUser : IdentityUser<int>
{
}
public class ApplicationRole : IdentityRole<int>
{
}
public class MyDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
{
}
答案 2 :(得分:-1)
是的,您可以自定义Identity中的默认类。我在Identity Framework上创建了一个易于使用的包装器,并创建了具有其他属性的自定义类。如果您想看一下,请访问blog