I'm trying to use the Identity package of .NET Core with multiple classes that extend IdentityUser<Guid>
but with a single UserRole
class.
I have multiple classes that extend UserStore<T>
for each user type and a single class that extends RoleStore<UserRole>
.
The following is my startup.cs
:
services.AddIdentity<InternalUser, UserRole>(IdentityOptions)
.AddDefaultTokenProviders()
.AddUserStore<InternalUserStore>()
.AddRoleStore<GenericUserRoleStore>();
services.AddIdentityCore<Contractor>(IdentityOptions)
.AddRoles<UserRole>()
.AddDefaultTokenProviders()
.AddUserStore<ContractorUserStore>()
.AddRoleStore<GenericUserRoleStore>();
services.AddIdentityCore<Homeowner>(IdentityOptions)
.AddRoles<UserRole>()
.AddDefaultTokenProviders()
.AddUserStore<HomeownerUserStore>()
.AddRoleStore<GenericUserRoleStore>();
My DbContext
is not extending IdentityDbContext
:
public sealed class EntityDbContext: DbContext { }
I was getting multiple errors so I added the following to DbContext
but I commented it out:
public DbSet<IdentityUserClaim<Guid>> UserClaims { get; set; }
public DbSet<IdentityUserRole<Guid>> UserRoles { get; set; }
I'm getting many different errors:
build Error on Instance 'Dal.IdentityStores.InternalUserStore' for PluginType IUserStore - and Instance 'RoleManager' for PluginType Microsoft.AspNetCore.Identity.RoleManager
1[Models.Entities.Users.UserRole] - and Instance 'Dal.IdentityStores.GenericUserRoleStore' for PluginType Microsoft.AspNetCore.Identity.IRoleStore
1[Models.Entities.Users.UserRole] - and Instance 'Dal.IdentityStores.GenericUserRoleStore' for PluginType Microsoft.AspNetCore.Identity.IRoleStore1[Models.Entities.Users.UserRole] - and Instance 'Dal.IdentityStores.ContractorUserStore' for PluginType Microsoft.AspNetCore.Identity.IUserStore
1[Models.Entities.Contractors.Contractor] - and Instance 'UserClaimsPrincipalFactory' for PluginType Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory1[Models.Entities.Contractors.Contractor] - and Instance 'UserClaimsPrincipalFactory<Contractor, UserRole>' for PluginType Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory
1[Models.Entities.Contractors.Contractor] - and Instance 'UserManager' for PluginType Microsoft.AspNetCore.Identity.UserManager1[Models.Entities.Homeowners.Homeowner] - and Instance 'UserClaimsPrincipalFactory<Homeowner>' for PluginType Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory
1[Models.Entities.Homeowners.Homeowner]
This is the link to my repo
I reproduced your problem and below is a solution to it, but I would think again about creating multiple tables for different user roles.
Here are two main reasons against multiple user tables:
In case you still want to have multiple tables for different user roles, here is a little "hack". You just need to override OnModelCreating method and configure entities:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Contractor>(b =>
{
b.HasMany<IdentityUserRole<Guid>>().WithOne().HasForeignKey(ur => ur.UserId).IsRequired();
});
builder.Entity<UserRole>(b =>
{
b.HasKey(r => r.Id);
b.HasIndex(r => r.NormalizedName).HasName("RoleNameIndex").IsUnique();
b.ToTable("AspNetRoles");
b.Property(r => r.ConcurrencyStamp).IsConcurrencyToken();
b.Property(u => u.Name).HasMaxLength(256);
b.Property(u => u.NormalizedName).HasMaxLength(256);
b.HasMany<IdentityUserRole<Guid>>().WithOne().HasForeignKey(ur => ur.RoleId).IsRequired();
b.HasMany<IdentityRoleClaim<Guid>>().WithOne().HasForeignKey(rc => rc.RoleId).IsRequired();
});
builder.Entity<IdentityRoleClaim<Guid>>(b =>
{
b.HasKey(rc => rc.Id);
b.ToTable("AspNetRoleClaims");
});
builder.Entity<IdentityUserRole<Guid>>(b =>
{
b.HasKey(r => new { r.UserId, r.RoleId });
b.ToTable("AspNetUserRoles");
});
builder.Entity<UserRole>().ToTable("Roles");
builder.Entity<IdentityUserRole<Guid>>().ToTable("UserRoles");
builder.Entity<IdentityRoleClaim<Guid>>().ToTable("RoleClaims");
builder.Entity<IdentityUserClaim<Guid>>().ToTable("UserClaims");
}
After that, you should be able to login.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With