Dan Chase
Dan Chase

Reputation: 1042

ASP.NET Migration expects AspNetUsers even though its renamed in OnModelCreate

I'm trying to rename my Identity 2.0 tables to have my app name before them. So I've overridden OnModelCreating:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<IdentityUser>().ToTable("appname_Users");
        modelBuilder.Entity<IdentityRole>().ToTable("appname_Roles");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("appname_UserClaims");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("appname_UserLogins");
        modelBuilder.Entity<IdentityUserRole>().ToTable("appname_UserRoles");
    }

I deleted the Migrations folder, made sure these tables did not exist, and ran:

enable-migrations -Force
update-database

When I try to login to the site, it still says it can't find dbo.AspNetUsers.

When I check the migration script, I do see the following:

  CreateTable(
            "dbo.AspNetUsers",
            c => new
                {
                    Id = c.String(nullable: false, maxLength: 128),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.appname_Users", t => t.Id)
            .Index(t => t.Id);

Where is it even getting the idea that it needs AspNetUsers? I've scoured documentation and can't find out what's going on. Any help would be appreciated.

Upvotes: 0

Views: 48

Answers (1)

Delete all your identity tables from database manually. Then in onModelCreating insert

        modelBuilder.Entity<ApplicationUser>().ToTable("appname_Users");
        modelBuilder.Entity<IdentityUserRole>().ToTable("appname_UserRoles");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("appname_UserLogins");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("appname_UserClaims");
        modelBuilder.Entity<IdentityRole>().ToTable("appname_Roles");

if you create custom identity classes then use

        modelBuilder.Entity<ApplicationUser>().ToTable("appname_Users");
        modelBuilder.Entity<ApplicationRole>().HasKey<string>(r => r.Id).ToTable("appname_Roles");
        modelBuilder.Entity<ApplicationUser>().HasMany<ApplicationUserRole>((ApplicationUser u) => u.UserRoles);
        modelBuilder.Entity<ApplicationUserRole>().HasKey(r => new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("appname_UserRoles");

This way works for me.........

Upvotes: 1

Related Questions