2

I have a standard Identity user in my app...

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{...

...Which has Id as primary key. How can I change Id to UserId?
I ask this because I have table that should be connected with AspNetUsers table. And I did it like this:

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{...
public virtual ICollection<JobApply> JobApplies { get; set; }

And JobApply code is:

public class JobApply
{...
public int ApplicationUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }

The result of this is this:
enter image description here
I want to make this better somehow.
I want to have UserId instead Id so columns in both table to be names UserId.
Is there a way to do this?

Community
  • 1
  • 1
1110
  • 7,829
  • 55
  • 176
  • 334

1 Answers1

2

You can't change the name. Id is defined at the very bottom of the extension system in IUser<T>, which all IdentityUser implementations must ultimately inherit from.

Just no way to do it without rewriting everything.

You COULD change the name of the field as it's stored in the database, but you can't change the name of the property as used in code. If you only want to change the name of the column in the database, then you need only use FluentConfiguration to change the name of the property.

EDIT:

In your ApplicationContext you would do this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<ApplicationUser>()
        .Property(p => p.Id)
        .HasColumnName("UserId");
}
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • You want to say that there is no way after generating database tables table `JobApply` have column `UserId` and it has to be `ApplicationUserId`? Off course without rewriting everyhing :) – 1110 Jun 29 '14 at 10:15
  • `then you need only use FluentConfiguration to change the name of the property` can you give me short example how to make this, please? – 1110 Jun 29 '14 at 10:18
  • @1110 - You weren't asking for JobApply to be UserId, you were asking for IdentityUser to have a different name for the Id. – Erik Funkenbusch Jun 29 '14 at 10:19
  • Another question rise from this answer http://stackoverflow.com/questions/24475351/asp-identity-table-column-change-but-mapping-is-not-possible-with-new-column-nam – 1110 Jun 29 '14 at 10:50
  • i have similar scenario but i want the userid value be entered in id when creating a user on [Register] http://stackoverflow.com/questions/42663471/add-primary-key-of-linked-table-to-aspnetusers-table-id-field can someone help please.. – Samra Mar 09 '17 at 03:09