我想将用户ID的数据类型从string更改为long。现在我有:
//ApplicationUser.cs
public class ApplicationUser : IdentityUser<long>
{
//some additional properties
}
public class ApplicationRole : IdentityRole<long>
{
}
//Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
//DbContext setup
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
//authentications, cookies and others
}
}
//ApplicationDbContext.cs
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, long>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
//fluent apis
}
//DBsets
}
就像this tutorial一样。但是当我运行应用程序时出现了这个错误。
TypeLoadException: GenericArguments[1], 'Microsoft.AspNetCore.Identity.IdentityRole', on
'Microsoft.AspNetCore.Identity.UserStoreBase`8[TUser,TRole,TKey,TUserClaim,TUserRole,TUserLogin,TUserToken,TRoleClaim]'
violates the constraint of type parameter 'TRole'.
我的理解是ApplicationRole的类型在制作UserStoreBase的实例时无效,但从未想出如何逃脱。
我环顾四周,发现了一些像this这样的文件,但是在2.0之前,我并不需要修改身份中的所有内容。这个过程可能有什么问题?该教程真的不够吗?
答案 0 :(得分:2)
也许这只是一个错字,但我认为你应该有:
services.AddIdentity<ApplicationUser, ApplicationRole>()
注意ApplicationRole
,而不是IdentityRole
。
UserStoreBase
(间接使用)期望TRole
的类型为IdentityRole<TKey>
,但您提供IdentityRole
。
ApplicationRole
的类型为IdentityRole<long>
,因此它应匹配。