Question
Question

Reputation: 170

Cannot create a DbSet for 'IdentityRoleClaim<int>'

I am trying to create a custom Identity for a already made Database and it is working for users logging in that do not have roles, but users with roles, it displays this error.

Cannot create a DbSet for 'IdentityRoleClaim' because this type is not included in the model for the context

Startup.cs

 services.AddIdentity<ApplicationIdentityUser, CustomRole>()
            .AddUserStore<CustomUserStore<ApplicationIdentityUser>>()
            .AddUserManager<CustomUserManager<ApplicationIdentityUser>>()
              .AddRoleStore<CustomRoleStore<ApplicationIdentityUser>>()
              .AddRoleManager<CustomRoleManager>()

            .AddEntityFrameworkStores<AhUserContext, int>()
            .AddDefaultTokenProviders();
        services.AddMvc()
          .AddViewLocalization(Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix)
          .AddDataAnnotationsLocalization();


        //Add the custom Password Hasher
        services.AddScoped<PasswordHasher<ApplicationIdentityUser>, CustomPasswordHasher<ApplicationIdentityUser>>();
        services.AddScoped<IdentityRole<int>, CustomRole>();
        services.AddScoped<IdentityRoleClaim<int>, CustomIdentityRoleClaim>();
        services.AddScoped<SignInManager<ApplicationIdentityUser>, CustomSignInManager<ApplicationIdentityUser>>();
        services.AddScoped<IdentityUserClaim<int>, CustomIdentityUserClaim>();
        services.AddScoped<IdentityUserLogin<int>, CustomIdentityUserLogin>();
        // Add application services.
        services.AddTransient<IEmailSender, AuthMessageSender>();
        services.AddTransient<ISmsSender, AuthMessageSender>();

        services.AddOptions();
        services.Configure<AppSettings>(Configuration.GetSection("MicrosoftAzureStorage"));

        services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();

Custom Identity Classes

public class ApplicationIdentityUser : IdentityUser<int>{} 
public class CustomIdentityRoleClaim : IdentityRoleClaim<int>
public class CustomIdentityUserClaim : IdentityUserClaim<int>
public class CustomIdentityUserRole : IdentityUserRole<int>
public class CustomRole : IdentityRole<int>
public class CustomRoleManager : RoleManager<CustomRole>
public class CustomRoleStore<ApplicationIdentityUser> : RoleStore<CustomRole, AhUserContext, int>
public class CustomSignInManager<IUser> : SignInManager<IUser> where IUser : ApplicationIdentityUser
public class CustomUserManager<IUser> : UserManager<IUser> where IUser : ApplicationIdentityUser
public class CustomUserStore<IUser> : UserStore<IUser, CustomRole, AhUserContext, int> where IUser : ApplicationIdentityUser

Upvotes: 0

Views: 1290

Answers (1)

Dave Jellison
Dave Jellison

Reputation: 933

Change:

public class CustomRoleStore<ApplicationIdentityUser> : RoleStore<CustomRole, AhUserContext, int>

to

public class CustomRoleStore<ApplicationIdentityUser> : RoleStore<CustomRole, AhUserContext, int, CustomIdentityUserRole, CustomIdentityRoleClaim >

I had the same issue. Because I had both a custom UserManager as well as RoleManager, both had to propagate up the inheritance chain properly for it all to work.

Upvotes: 1

Related Questions