【发布时间】:2017-01-25 16:43:35
【问题描述】:
我正在尝试使用以下实体自定义 asp.net 身份实体:
public class User : IdentityUser<string, UserClaim, UserRole, UserLogin>
public class UserClaim : IdentityUserClaim<string>
public class UserLogin : IdentityUserLogin<string>
public class UserRole : IdentityUserRole<string>
public class UserToken : IdentityUserToken<string>
public class Role : IdentityRole<string, UserRole, RoleClaim>
public class RoleClaim : IdentityRoleClaim<string>
然后我创建了一个如下所示的 DbContext 类
public class AppDbContext : IdentityDbContext<User, Role, string, UserClaim, UserRole, UserLogin, RoleClaim, UserToken>
然后用
配置这些services
.AddIdentity<User, Role>()
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();
但我也尝试过
.AddEntityFrameworkStores<AppDbContext, string>()
我在互联网上阅读了许多博客文章,例如 this,但其中大多数都必须处理键数据类型的更改,例如 int 或 Guid。在我的情况下,我不会从 string 更改默认键数据类型。
我在所有这些情况下编译都可以,但是运行时会抛出错误
System.TypeLoadException GenericArguments[0],'MyIdentity.Entities.User', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`8[TUser,TRole,TContext,TKey,TUserClaim,TUserRole,TUserLogin,TUserToken]' 违反类型参数'TUser'的约束。
在 System.RuntimeTypeHandle.Instantiate(RuntimeTypeHandle 句柄, IntPtr* pInst,int numGenericArgs,ObjectHandleOnStack 类型)在 System.RuntimeTypeHandle.Instantiate(Type[] inst) 在 System.RuntimeType.MakeGenericType(Type[] 实例化)
如果我添加一个自定义的UserStore 类,如this post 中所述
public class UserStore : UserStore<User, Role, AppDbContext, string, UserClaim, UserRole, UserLogin, UserToken>
出现编译错误提示
CS0311 类型“MyIdentity.Entities.Role”不能用作类型 泛型类型或方法“UserStore”中的参数“TRole”。没有隐式引用转换 'MyIdentity.Entities.Role' 到 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole>'
我做错了什么?
【问题讨论】:
-
你的
User类有默认构造函数吗? -
@TheodorosChatzigiannakis:是的。我刚刚检查过了。
public User() : base()
标签: asp.net-core asp.net-identity asp.net-identity-3