Entity Framework mapping between ASP Identity user and other tables - change Id column

asp.net asp.net-identity c# entity-framework entity-framework-6

Question

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?

1
1
6/29/2014 9:54:42 AM

Accepted Answer

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");
}
2
6/29/2014 10:21:59 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow