ash
ash

Reputation: 3085

Unable to change Asp Identity Table Names..... Asp .Net Core 2?

I want to change the identity table's name. I googled around and found the following methods:

The first one:

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);


    builder.Entity<ApplicationUser>(entity =>
    {
        entity.ToTable(name: "Users");
    });

    builder.Entity<ApplicationRole>(entity =>
    {
        entity.ToTable(name: "Roles");
    });

    builder.Entity<ApplicationRoleClaim>(entity =>
    {
        entity.ToTable(name: "RoleClaims");
    });

    builder.Entity<ApplicationUserRole>(entity =>
    {
        entity.ToTable(name: "UserRoles");
    });

    builder.Entity<ApplicationUserLogin>(entity =>
    {
        entity.ToTable(name: "UserLogins");
    });

    builder.Entity<ApplicationUserClaim>(entity =>
    {
        entity.ToTable(name: "UserClaims");
     });

    builder.Entity<ApplicationUserToken>(entity =>
    {
        entity.ToTable(name: "UserTokens");
    });
 }

The second one:

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<ApplicationUser>().ToTable("Users");
        builder.Entity<ApplicationRole>().ToTable("Roles");
        builder.Entity<ApplicationRoleClaim>().ToTable("RoleClaims");
        builder.Entity<ApplicationUserRole>().ToTable("UserRoles");
        builder.Entity<ApplicationUserClaim>().ToTable("UserClaims");
        builder.Entity<ApplicationUserToken>().ToTable("UserTokens");
        builder.Entity<ApplicationUserLogin>().ToTable("UserLogins");
    }

All the generic names, like ApplicationUser, ApplicationRole, etc., have "int" as their primary key.

The ApplicationDbContext and StartUp looks like the following

ApplicationDbContext

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>

StartUp class

       services.AddDbContext<ApplicationDbContext>(options =>
            options.UseMySql(Configuration.GetConnectionString("MySQLConnection")));

        services.AddIdentity<ApplicationUser, ApplicationRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

If you noticed, on the start-up class, I didn't add an int on AddEntityFrameworkStores<ApplicationDbContext>(). Because, a compiler error keeps showing up saying "Not supported....".

And I did the following to remove the old database and migration

$ drop-database
$ remove-migration

To add the new configuration

$ add-migration Initial
$ update-database

I found that only the users and roles tables are changed, but not the others (AspUserRoles, AspUserClaim, etc.).

FYI:

I am using Visual Studio 2017. My project uses the default Web Application with Individual User Account selected and .NET Core 2. I am also using Pomela.EntityFramework.MySql as my database provider.

My question is: "What am I doing wrong?" Or what's changed?

Upvotes: 1

Views: 965

Answers (2)

Kevy Granero
Kevy Granero

Reputation: 783

You need to do the following steps:

  1. Add the table names you want as per code below:

    protected override void OnModelCreating(ModelBuilder builder)
    {
       base.OnModelCreating(builder);    
       builder.Entity<ApplicationUser>().ToTable("Users");
       builder.Entity<ApplicationRole>().ToTable("Roles");
       builder.Entity<ApplicationRoleClaim>().ToTable("RoleClaims");
       builder.Entity<ApplicationUserRole>().ToTable("UserRoles");
       builder.Entity<ApplicationUserClaim>().ToTable("UserClaims");
       builder.Entity<ApplicationUserToken>().ToTable("UserTokens");
       builder.Entity<ApplicationUserLogin>().ToTable("UserLogins");
    }
    
  2. Open the Package Manager Console and run the following command:

    Add-Migration MyTablesRenamed

    You will see a new file created in the application with all commands to rename the tables and some more commands.

  3. Run Update-Database

Done, your tables should be renamed now.

Upvotes: 0

Kirk Larkin
Kirk Larkin

Reputation: 93003

You need to tell IdentityDbContext about all of your custom types. In order to do that, you'll need to expand out the generics you pass through to IdentityDbContext, like so:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser,
    ApplicationRole, int, ApplicationUserClaim, ApplicationUserRole,
    ApplicationUserLogin, ApplicationRoleClaim, ApplicationUserToken>

Upvotes: 4

Related Questions