Why does this violate the type constraint?

前端 未结 3 1531
伪装坚强ぢ
伪装坚强ぢ 2020-12-06 00:19

I\'m trying to customise ASP.NET Identity 3 so that it uses integer keys:

public class ApplicationUserLogin : IdentityUserLogin { }
public class Ap         


        
相关标签:
3条回答
  • 2020-12-06 00:22

    Ran into this problem as well. I had to add IdentityRole key type also, because it was still throwing the same error.

            services.AddIdentity<ApplicationUser, IdentityRole<int>>()
                .AddEntityFrameworkStores<ApplicationDbContext,int>()
                .AddDefaultTokenProviders();
    
    0 讨论(0)
  • 2020-12-06 00:30

    Note for EF Core Users

    Just to add to the above, if you are using .Net core 3.0 (not sure about earlier versions), there is no longer a AddEntityFrameworkStores<TContext,TKey> method.

    Instead there is a generic variant of IdentityDbContext so instead you derive your DbContext from IdentityDbContext<TUser,TRole,TKey>

    e.g. in my case

    class ApplicationUser : IdentityUser<int> {...}
    class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<int>, int> {...}
    

    then in your startup you can use services.AddDefaultIdentity<ApplicationUser>

    cribbed from the last comment in https://github.com/aspnet/Identity/issues/1082

    0 讨论(0)
  • 2020-12-06 00:43

    Ran into this problem. It was crashing on the startup.cs file. changed

    services.AddIdentity<ApplicationUser, ApplicationIdentityRole>()
                    .AddEntityFrameworkStores<ApplicationDbContext>()
                    .AddDefaultTokenProviders();
    

    to

    services.AddIdentity<ApplicationUser, ApplicationIdentityRole>()
                    .AddEntityFrameworkStores<ApplicationDbContext,int>()
                    .AddDefaultTokenProviders();
    

    declaring the key type seemed to get past the crash

    0 讨论(0)
提交回复
热议问题