20

I followed the advice in this question to rename my ASP.NET Identity tables:

modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaim");
modelBuilder.Entity<IdentityUserRole>().ToTable("UserRole");
modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin");
modelBuilder.Entity<IdentityRole>().ToTable("Role");
modelBuilder.Entity<IdentityUser>().ToTable("User");
modelBuilder.Entity<ApplicationUser>().ToTable("User");

However this results in two properties for the UserClaim to User relation - UserId and IdentityUser_Id:

enter image description here

Is there any way I can fix this?

Community
  • 1
  • 1
Jonathan
  • 13,947
  • 17
  • 94
  • 123
  • 1
    One Foreign Key one Primary Key what is the issue? – Mark Homer Mar 10 '14 at 13:45
  • 1
    the problem here is that the UserId field is the one that should be the foreign key not the auto generated 'IdentityUser_Id' so the foreign key relationship with the Users table is broken. I have encountered this issue but never managed to escape it with my custom names in place i had to revert back to the default name,, if you figured it out please let me know,,, oh one more thing i could not re-set the foreign key using the fluent API . @MarkHomer – a7madx7 Mar 17 '14 at 04:29
  • 1
    @Mark `IdentityUser_Id` should not be there as it's an unnecessary duplicate of `UserId`. `UserId` should be the foreign key. – Jonathan Apr 18 '14 at 09:29

1 Answers1

28

You should skip the following line, then it works perfectly well:

modelBuilder.Entity<IdentityUser>().ToTable("User");

So the only mapping you need is:

modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaim");
modelBuilder.Entity<IdentityUserRole>().ToTable("UserRole");
modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin");
modelBuilder.Entity<IdentityRole>().ToTable("Role");
modelBuilder.Entity<ApplicationUser>().ToTable("User");

Hope that helps.

Łukasz Stempek
  • 421
  • 6
  • 8
  • Hunted high and low for a resolution to this problem! Worked perfectly, many thanks – Jon Selby Feb 02 '15 at 11:39
  • 1
    Spent way too long on this. I kept trying to add my own foreign keys and could not believe it was a simple change. This [SO post](http://stackoverflow.com/questions/19460386/how-can-i-change-the-table-names-when-using-visual-studio-2013-aspnet-identi) lead me to believe it was required. – Matt Overall Sep 05 '15 at 22:20
  • 3
    There was a [msdn blog](https://blogs.msdn.microsoft.com/webdev/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates/) (see comment section @Giang) that had code sample on how to rename `AspNetUsers` table that I think a lot of people saw, which led them to believe adding `modelBuilder.Entity().ToTable("User");` was the right way, when actually it isn't. – kimbaudi Oct 13 '16 at 06:58