IdentityServer update database error when multiple user types.
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
Here are the relevant classes:
EntityUser.cs
ClubAdmin.cs
DJ.cs
add-migration Test
and run update-database
I get the following error:
There is already an object named 'AspNetRoles' in the database.
There is already an object named 'AspNetRoles' in the database.
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>();
}
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>();
}
public class ClubAdmin : EntityUser
{
public virtual ICollection<Club> Clubs { get; set; } = new List<Club>();
// other clubadmin specific properties
}
public class ClubAdmin : EntityUser
{
public virtual ICollection<Club> Clubs { get; set; } = new List<Club>();
// other clubadmin specific properties
}
public class DJ : EntityUser
{
public virtual ICollection<Club> Clubs { get; set; } = null!;
public PayoutMethod PayoutMethod { get; set; }
//other specific properties
}
public class DJ : EntityUser
{
public virtual ICollection<Club> Clubs { get; set; } = null!;
public PayoutMethod PayoutMethod { get; set; }
//other specific properties
}
1 Reply
here is where i register identityserver
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;
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;