Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the table names used by asp.net identity 3 (vnext)?

The method used in asp.net identity 2 to alter the identity table names does not work in asp.net identity 3.

like image 756
Joseph Bailey Avatar asked Dec 30 '15 04:12

Joseph Bailey


2 Answers

You can do this easily by changing the entity mapping with extension method ToTable("TableName")on OnModelCreating of your DbContext:

And you don't need to use .ForSqlServerToTable(), just .ToTable() should work in any database.

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

    builder.Entity<User>().ToTable("Users"); // Your custom IdentityUser class
    builder.Entity<IdentityUserLogin<string>>().ToTable("UserLogins");
    builder.Entity<IdentityUserToken<string>>().ToTable("UserTokens");
    builder.Entity<IdentityUserClaim<string>>().ToTable("UserClaims");
    builder.Entity<IdentityUserRole<string>>().ToTable("UserRoles");
    builder.Entity<IdentityRoleClaim<string>>().ToTable("RoleClaims");
    builder.Entity<IdentityRole>().ToTable("Roles");            
}

The only catch here is to remember to use the generics with the type of your identifier (string is default on AspNetCore.

like image 110
Tanato Avatar answered Oct 19 '22 04:10

Tanato


Modify the builder entities in OnModelCreating of your ApplicationDbContext, using ForSqlServerToTable extension method to change the desired table(s) name.

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);

            builder.Entity<ApplicationUser>().ForSqlServerToTable("Users");
            builder.Entity<IdentityUserRole<string>>().ForSqlServerToTable("UserRoles");
            builder.Entity<IdentityUserLogin<string>>().ForSqlServerToTable("UserLogins");
            builder.Entity<IdentityUserClaim<string>>().ForSqlServerToTable("UserClaims");
            builder.Entity<IdentityRole>().ForSqlServerToTable("Roles");                        
        }
    }
like image 34
Joseph Bailey Avatar answered Oct 19 '22 04:10

Joseph Bailey