Search code examples
c#asp.netasp.net-mvcasp.net-identity

How to change table names in Asp.net Identity 3.0?


How to change table names in ASP.net Identity 3.0?

I have searched but I didn't get any workable write up for Identity 3.0

and this How can I change the table names used by asp.net identity 3 (vnext)? is not working.


Solution

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

    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.