How can I change default ASP.NET Identity table names in .NET CORE?
Try to change binding to
builder.Entity<ApplicationUser>(entity =>
{
entity.ToTable(name:"Users");
entity.Property(e => e.Id).HasColumnName("UserId");
});
-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 });
});
}