Reputation: 187
When i try to rename table's names i use this code below. All works good. but when i update the database, it has some old tables. for example:
AspNetUserRoles
and new tables UserRoles
. Why is this happening? thanks
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customizations
builder.Entity<User>(i =>
{
i.ToTable("Users");
i.HasKey(x => x.Id);
});
builder.Entity<IdentityRole>(i =>
{
i.ToTable("Roles");
i.HasKey(x => x.Id);
});
builder.Entity<IdentityUserRole<Guid>>(i =>
{
i.ToTable("UserRoles");
i.HasKey(x => new { x.RoleId, x.UserId });
});
builder.Entity<IdentityUserLogin<Guid>>(i =>
{
i.ToTable("UserLogins");
i.HasKey(x => new { x.ProviderKey, x.LoginProvider });
});
builder.Entity<IdentityRoleClaim<Guid>>(i =>
{
i.ToTable("RoleClaims");
i.HasKey(x => x.Id);
});
builder.Entity<IdentityUserClaim<Guid>>(i =>
{
i.ToTable("UserClaims");
i.HasKey(x => x.Id);
});
builder.Entity<IdentityUserToken<Guid>>(i =>
{
i.ToTable("UserTokens");
i.HasKey(x => x.UserId);
});
}
Upvotes: 1
Views: 680
Reputation: 2282
This is how I do it and it works fine with migrations/update.
base.OnModelCreating(builder);
builder.Entity<User>().ToTable("Users");
builder.Entity<IdentityRole>().ToTable("Roles");
builder.Entity<IdentityUserRole<string>>().ToTable("UserRoles");
builder.Entity<IdentityUserClaim<string>>().ToTable("UserClaims");
builder.Entity<IdentityUserLogin<string>>().ToTable("UserLogins");
builder.Entity<IdentityRoleClaim<string>>().ToTable("RoleClaims");
builder.Entity<IdentityUserToken<string>>().ToTable("UserTokens");
Also note that I don't use the Guid
type. Microsoft uses string
for their Id columns so we'll use that too.
Upvotes: 2