代码之家  ›  专栏  ›  技术社区  ›  kemakino

正在更改asp中用户id的数据类型。net core 2.0运行不正常

  •  0
  • kemakino  · 技术社区  · 8 年前

    我想将用户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之前的版本,我不需要修改标识中的所有内容。这个过程中可能有什么错误?教程还不够吗?

    1 回复  |  直到 8 年前
        1
  •  2
  •   Gabriel Luci    8 年前

    也许这只是一个输入错误,但我认为你应该:

    services.AddIdentity<ApplicationUser, ApplicationRole>()
    

    笔记 ApplicationRole IdentityRole .

    UserStoreBase (间接使用)预期 TRole 类型为 IdentityRole<TKey> ,但你给了它 识别符 .

    属于类型 IdentityRole<long> 所以应该匹配。