3

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);
            });
Svinjica
  • 2,389
  • 2
  • 37
  • 66
  • Where is this `AddCustomIdentityConfiguration` method called? If called properly, there is nothing more you need to do in order to access the "renamed" table. – Ivan Stoev Oct 13 '21 at 17:38
  • @IvanStoev I've updated question. It was basically triggered with initial migration – Svinjica Oct 13 '21 at 17:43
  • I mean, where this method is called *in your code*. i.e. from inside `OnModelCreating`, before or after base call (in case you are inheriting one of the base generic `IdentityDbContext`) – Ivan Stoev Oct 13 '21 at 17:48
  • It is called inside OnModelCreating `protected override void OnModelCreating(ModelBuilder builder) { builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly()); base.OnModelCreating(builder); builder.AddCustomIdentityConfiguration(); }` – Svinjica Oct 13 '21 at 17:54
  • 1
    Hmm, then it should work. Except if the `RoleManager` class you are passing in is not using your db context (with the aforementioned `OnModelCreating` override). – Ivan Stoev Oct 13 '21 at 18:14
  • can you share your startup class – Elyas Esna Oct 13 '21 at 19:35

0 Answers0