IdentityServer with inheritance design. Update-database error

24 views Asked by At

I am using IdentityServer in dotnet. I have a base type EntityUser that inherits the IdentityUser<int> class. The entityuser is a base class for multiple user types.

After creating a migration successfully with add-migration Test and run update-database I get the following error:

There is already an object named 'AspNetRoles' in the database.

Here are the relevant classes:

EntityUser.cs

public class EntityUser : IdentityUser<int>
{
    public bool Active { get; set; }
    public decimal Credits { get; set; } = 0;
    public virtual ICollection<EntityUserRole> UserRoles { get; set; } = new List<EntityUserRole>();
}

ClubAdmin.cs

public class ClubAdmin : EntityUser
{
    public virtual ICollection<Club> Clubs { get; set; } = new List<Club>();
    // other clubadmin specific properties
}

DJ.cs

public class DJ : EntityUser
{
    public virtual ICollection<Club> Clubs { get; set; } = null!;
    public PayoutMethod PayoutMethod { get; set; }
    //other specific properties 
}

My dependancy injection class where i register identity server:

public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
{
    services.AddDbContext<DataContext>(options =>
    options.UseSqlServer(
            configuration.GetConnectionString("DefaultConnection"),
            b => b.MigrationsAssembly(typeof(DataContext).Assembly.FullName)));

    services.AddScoped<IDataContext>(provider => provider.GetService<DataContext>()!);

    services.AddIdentity<EntityUser, Role>(opt =>
    {
        opt.Password.RequiredLength = 8;
        opt.Password.RequireDigit = false;
        opt.Password.RequireNonAlphanumeric = false;
        opt.Password.RequireUppercase = false;
        opt.User.RequireUniqueEmail = true;
        opt.User.AllowedUserNameCharacters = null;
    })
    .AddEntityFrameworkStores<DataContext>();

    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
    })
    .AddJwtBearer(options =>
    {
        options.SaveToken = true;
        options.RequireHttpsMetadata = false;
        options.TokenValidationParameters = new TokenValidationParameters()
        {
            ValidateIssuer = true,
            ValidateAudience = true,
            ValidAudience = configuration["JWTSettings:validAudience"],
            ValidIssuer = configuration["JWTSettings:validIssuer"],
            IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["JWTSettings:securityKey"]!))
        };
    });
    return services;

I have tried changing the dependancy injection class for each type to implement this code:

services.AddIdentityCore<DJ>(opt =>
    {
        opt.Password.RequiredLength = 8;
        opt.Password.RequireDigit = false;
        opt.Password.RequireNonAlphanumeric = false;
        opt.Password.RequireUppercase = false;
        opt.User.RequireUniqueEmail = true;
        opt.User.AllowedUserNameCharacters = null;
    })
        .AddEntityFrameworkStores<DataContext>();
0

There are 0 answers