Reputation: 4325
I've started with .NET Core, in MVC 5 I changed default table names for example: AspNETUsers to Users in this way and it worked perfectly: In IdentityModels Class I dded:
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().ToTable("Users").Property(p => p.Id).HasColumnName("UserId");
modelBuilder.Entity<ApplicationUser>().ToTable("Users").Property(p => p.Id).HasColumnName("UserId");
modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles");
modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins");
modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims");
modelBuilder.Entity<IdentityRole>().ToTable("Roles");
}
But it does not work in ASP.NET CORE (MVC 6). Can Anyone help me? Thanks a lot.
Upvotes: 48
Views: 36602
Reputation: 314
In Entity Framework Core Version 6 this works fine:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
foreach (var entity in builder.Model.GetEntityTypes())
{
// Replace table names
entity.SetTableName(entity.GetTableName().ToSnakeCase());
}
}
Upvotes: 1
Reputation: 97
To update default table names IdentityModels Class MVC
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().ToTable("Users").HasKey(x => x.Id);
modelBuilder.Entity<ApplicationUser>().ToTable("Users").HasKey(x => x.Id);
modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles").HasKey(x => x.RoleId);;
modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins").HasKey(x => x.UserID);;
modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims").HasKey(x => x.Id);;
modelBuilder.Entity<IdentityRole>().ToTable("Roles").HasKey(x => x.Id);;
}
Upvotes: 1
Reputation: 476
I am adding a second answer, because many people will hit this when trying to change table names in .NET core 1, 2, 3 and .NET 5
The process is pretty straightforward and well explained in the docs.
For those needing quick fix:
Inherit all your models which you want to change( default Microsoft authorization comes with 7 models ). For example changing AspNetUsers to User and AspNetRoles to Role you can do the following in your existing models:
public partial class User : IdentityUser<int>
{
// code
}
public partial class Role : IdentityRole<int>
{
// code
}
In this example I am changing the primary key type also because the default is nvarchar
.
In your context inherit from IdentityDbContext and use the same type as argument:
public class AppDbContext : IdentityDbContext<User, Role, int>
{
// code
}
Next we have to update ConfigureServices
in StartUp
to use the new User
class:
services.AddDefaultIdentity<User, Role>()
.AddEntityFrameworkStores<AppDbContext>();
Then if want need you migrate to update/create database. Depends if this is a new or old project.
Note: If you are currently using authentication in your project like
UserManager
orSignInManager
you have to change their generic arguments to the new ones like so:
SignInManager<User>
UserManager<User>
Hope that helps :)
Upvotes: 2
Reputation: 2326
There is one extra note :
I customized all of my identity tables name but it didn't apply. base on Microsoft reference you have to use base.OnModelCreating(builder);
before your customization
To change the names of tables and columns, call
base.OnModelCreating
. Then, add configuration to override any of the defaults. For example, to change the name of all the Identity tables:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>(b =>
{
b.ToTable("MyUsers");
});
modelBuilder.Entity<IdentityUserClaim<string>>(b =>
{
b.ToTable("MyUserClaims");
});
modelBuilder.Entity<IdentityUserLogin<string>>(b =>
{
b.ToTable("MyUserLogins");
});
modelBuilder.Entity<IdentityUserToken<string>>(b =>
{
b.ToTable("MyUserTokens");
});
modelBuilder.Entity<IdentityRole>(b =>
{
b.ToTable("MyRoles");
});
modelBuilder.Entity<IdentityRoleClaim<string>>(b =>
{
b.ToTable("MyRoleClaims");
});
modelBuilder.Entity<IdentityUserRole<string>>(b =>
{
b.ToTable("MyUserRoles");
});
}
Upvotes: 2
Reputation: 628
Just for documentation purpose, for the one who comes to this post on the years anyears on the future, (like me XD), The answer to the question:
How can I change default ASP.NET Identity table names in .NET CORE?
Can be solved as this
//Repeat with each table
builder.Entity<ApplicationUser>(entity =>
{
entity.ToTable(name:"Users");
entity.Property(e => e.Id).HasColumnName("UserId");
});
Or can be solved like this
modelBuilder.Entity<IdentityUser>().ToTable("MyUsers").Property(p => p.Id).HasColumnName("UserId");
modelBuilder.Entity<ApplicationUser>().ToTable("MyUsers").Property(p => p.Id).HasColumnName("UserId");
modelBuilder.Entity<IdentityUserRole>().ToTable("MyUserRoles");
modelBuilder.Entity<IdentityUserLogin>().ToTable("MyUserLogins");
modelBuilder.Entity<IdentityUserClaim>().ToTable("MyUserClaims");
modelBuilder.Entity<IdentityRole>().ToTable("MyRoles");
But you can simplyfied with this method given by Alexandru Bucur on his blog and tested on netcore 2.2
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
var table = entityType.Relational().TableName;
if (table.StartsWith("AspNet"))
{
entityType.Relational().TableName = table.Substring(6);
}
};
But this it's not longger support on netcore > 2.2, so, I need to fix it and this is the functional way on NetCore > 2.2
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
var tableName = entityType.GetTableName();
if (tableName.StartsWith("AspNet"))
{
entityType.SetTableName(tableName.Substring(6));
}
}
Choose what you prefeer and enjoy it, HappyCoding
Upvotes: 7
Reputation: 295
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>(b =>
{
b.ToTable("ANYName");
});
modelBuilder.Entity<IdentityUserRole<string>>(b =>
{
b.ToTable("ANYName");
});
modelBuilder.Entity<IdentityRole<string>>(b =>
{
b.ToTable("ANYName");
});
modelBuilder.Entity<IdentityUserClaim<string>>(b =>
{
b.ToTable("ANYName");
});
modelBuilder.Entity<IdentityUserLogin<string>>(b =>
{
b.ToTable("ANYName");
});
modelBuilder.Entity<IdentityUserToken<string>>(b =>
{
b.ToTable("ANYName");
});
modelBuilder.Entity<IdentityRole>(b =>
{
b.ToTable("ANYName");
});
modelBuilder.Entity<IdentityRoleClaim<string>>(b =>
{
b.ToTable("ANYName");
});
modelBuilder.Entity<IdentityUserRole<string>>(b =>
{
b.ToTable("ANYName");
});
}
Add this method in ApplicationDbContext and add-migration to create changes and then update database to save changes.
Upvotes: 5
Reputation: 1329
A complete list for ASP.Net Core 2/2.1, based on @ahmed-al-jabry 's answer.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Override default AspNet Identity table names
modelBuilder.Entity<User>(entity => { entity.ToTable(name: "Users"); });
modelBuilder.Entity<IdentityRole>(entity => { entity.ToTable(name: "Roles"); });
modelBuilder.Entity<IdentityUserRole<string>>(entity => { entity.ToTable("UserRoles"); });
modelBuilder.Entity<IdentityUserClaim<string>>(entity => { entity.ToTable("UserClaims"); });
modelBuilder.Entity<IdentityUserLogin<string>>(entity => { entity.ToTable("UserLogins"); });
modelBuilder.Entity<IdentityUserToken<string>>(entity => { entity.ToTable("UserTokens"); });
modelBuilder.Entity<IdentityRoleClaim<string>>(entity => { entity.ToTable("RoleClaims"); });
}
Upvotes: 17
Reputation: 97
For MVC5 IdentityModels
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<IdentityUser>().Table("Users").Key(x => x.Id);
builder.Entity<ApplicationUser>().Table("Users").Key(x => x.Id);
builder.Entity<IdentityUserRole>().Table("UserRoles").Key(x => x.RoleId); ;
builder.Entity<IdentityUserLogin>().Table("UserLogins").Key(x => x.UserId); ;
builder.Entity<IdentityUserClaim>().Table("UserClaims").Key(x => x.Id); ;
builder.Entity<IdentityRole>().Table("Roles").Key(x => x.Id); ;
}
Upvotes: 0
Reputation: 287
There are further 2 steps required to apply changes:
Upvotes: 2
Reputation: 1467
-To change the names of those tables ( IdentityUserRole<Tkey>
, IdentityUserToken<Tkey>
, IdentityRoleClaim<Tkey>
, IdentityUserClaim<Tkey>
, IdentityUserLogin<Tkey>
) you have to specify the type of TKey(The type used for the primary key ) which is string(GUID) by default even if you didn't change it.
-If you want to change the primary key from GUID to int https://medium.com/@goodealsnow/asp-net-core-identity-3-0-6018fc151b4
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>(entity =>
{
entity.ToTable(name: "User");
});
builder.Entity<IdentityRole>(entity =>
{
entity.ToTable(name: "Role");
});
builder.Entity<IdentityUserRole<string>>(entity =>
{
entity.ToTable("UserRoles");
//in case you chagned the TKey type
// entity.HasKey(key => new { key.UserId, key.RoleId });
});
builder.Entity<IdentityUserClaim<string>>(entity =>
{
entity.ToTable("UserClaims");
});
builder.Entity<IdentityUserLogin<string>>(entity =>
{
entity.ToTable("UserLogins");
//in case you chagned the TKey type
// entity.HasKey(key => new { key.ProviderKey, key.LoginProvider });
});
builder.Entity<IdentityRoleClaim<string>>(entity =>
{
entity.ToTable("RoleClaims");
});
builder.Entity<IdentityUserToken<string>>(entity =>
{
entity.ToTable("UserTokens");
//in case you chagned the TKey type
// entity.HasKey(key => new { key.UserId, key.LoginProvider, key.Name });
});
}
Upvotes: 38
Reputation: 49789
Try to change binding to
builder.Entity<ApplicationUser>(entity =>
{
entity.ToTable(name:"Users");
entity.Property(e => e.Id).HasColumnName("UserId");
});
Upvotes: 32