Invalid object name AspNetRoles .NET Core 5

Viewed 234

I'm getting exception Invalid object name 'AspNetRoles'. when I'm trying to seed values at the app start. I believe this is because I've created custom configuration that renamed Identity tables, using this approach: I have to point out that I've used code first approach.

  public static void AddCustomIdentityConfiguration(this ModelBuilder builder)
        {
            builder.Entity<ApplicationUser>().ToTable("users"); //AspNetUsers
            builder.Entity<IdentityRole>().ToTable("roles"); //AspNetRoles
            builder.Entity<IdentityUserRole<string>>().ToTable("user_roles"); //AspNetUserRole
            builder.Entity<IdentityUserClaim<string>>().ToTable("user_claims"); //AspNetUserClaim
            builder.Entity<IdentityUserLogin<string>>().ToTable("user_login"); //AspNetUserLogin
            builder.Entity<IdentityRoleClaim<string>>().ToTable("role_claim"); //AspNetRoleClaim
            builder.Entity<IdentityUserToken<string>>().ToTable("user_token"); //AspNetUserToken
        }

I created simple static method that should do the seeding

public static async Task SeedDBDefaultRolesAsync(RoleManager<IdentityRole> roleManager)
        {

            if (!roleManager.Roles.Any())
            {
                var roles = new List<RoleDTO>()
                {
                    new RoleDTO() { Name = "Admin", NormalizedName = "ADMIN"},
                    new RoleDTO() { Name = "User", NormalizedName = "USER"},
                };

                foreach (var role in roles)
                {
                    await roleManager.CreateAsync(role);
                }
            }
        }

So, my question is this how can I access roles table instead of AspNetRoles table?

UPDATE: AddCustomIdentityConfiguration is triggered on add-migration. It was created with initial migration

Example of migration..

....migrationBuilder.CreateTable(
            name: "users",
            columns: table => new
            {
                Id = table.Column<string>(type: "nvarchar(450)", nullable: false),
                UserName = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true),
                NormalizedUserName = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true),
                Email = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true),
                NormalizedEmail = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true),
                EmailConfirmed = table.Column<bool>(type: "bit", nullable: false),
                PasswordHash = table.Column<string>(type: "nvarchar(max)", nullable: true),
                SecurityStamp = table.Column<string>(type: "nvarchar(max)", nullable: true),
                ConcurrencyStamp = table.Column<string>(type: "nvarchar(max)", nullable: true),
                PhoneNumber = table.Column<string>(type: "nvarchar(max)", nullable: true),
                PhoneNumberConfirmed = table.Column<bool>(type: "bit", nullable: false),
                TwoFactorEnabled = table.Column<bool>(type: "bit", nullable: false),
                LockoutEnd = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                LockoutEnabled = table.Column<bool>(type: "bit", nullable: false),
                AccessFailedCount = table.Column<int>(type: "int", nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_users", x => x.Id);
            });
0 Answers
Related