1

I have changed the asp Identity so that in database Id column of AspNetIdentity table be UserId:

modelBuilder.Entity<ApplicationUser>()
                        .Property(p => p.Id)
                        .HasColumnName("UserId");

And that works fine. Generated table have UserId instead Id.
Now I have another table that should be mapped with AspNetUsers by UserId:
When write it this way:

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

Table in database looks like:
enter image description here
And if I write it like this:

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

database looks like:
enter image description here


First option created UserId field but it is not FK instead of that new field is added as FK.
How can I properly map this to have table JobApply with field UserId to be FK to AspNetUsers?

UPDATE

When I add this:

base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<ApplicationUser>()
                        .Property(p => p.Id)
                        .HasColumnName("UserId");

            modelBuilder.Entity<JobApply>()
                    .HasRequired(e => e.ApplicationUser)
                    .WithMany()
                    .HasForeignKey(e => e.UserId);

Tables relation looks like this:
enter image description here
This is both classes:

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
    {
        public string Email { get; set; }
        public string ConfirmationToken { get; set; }
        public bool IsConfirmed { get; set; }
        public string PasswordResetToken { get; set; }

        public int CityId { get; set; }
        public virtual City City { get; set; }


        public virtual ICollection<JobApply> JobApplies { get; set; }
    }

public class JobApply
    {
        public int JobApplyId { get; set; }
        public int UserId { get; set; }
        public int JobId { get; set; }
        public int JobApplyStatusId { get; set; }
        public DateTime CreatedDate { get; set; }

        public virtual ApplicationUser ApplicationUser { get; set; }
        public virtual Job Job { get; set; }
        public virtual JobApplyStatus JobApplyStatus { get; set; }

    }
1110
  • 7,829
  • 55
  • 176
  • 334

1 Answers1

2

Define your relationship with the fluent API like this:

modelBuilder.Entity<JobApply>()
                    .HasRequired(e => e.ApplicationUser)
                    .WithMany(e => e.JobApplies)
                    .HasForeignKey(e => e.UserId);

You may as well remove the property UserId from the ApplicationUser class and then define the mapping like this:

modelBuilder.Entity<JobApply>()
                    .HasRequired(e => e.ApplicationUser)
                    .WithMany(e => e.JobApplies)
                    .Map(m => m.MapKey("UserId"));
svlasov
  • 9,923
  • 2
  • 38
  • 39
mr100
  • 4,340
  • 2
  • 26
  • 38
  • With this `UserId` becomes FK but field `ApplicationUser_Id` is also in the table? – 1110 Jun 29 '14 at 11:13
  • No, if you define it like that ApplicationUser_Id won't be added any more as it won't be needed. ApplicationUser_Id is default name for ef for foreign key property to ApplicationUser class. If you specify this name yourself then it will override this default name. – mr100 Jun 29 '14 at 11:16
  • But, if I remove `public virtual ICollection JobApplies { get; set; }` from `ApplicationUser` than I get only `UserId`. Now I am still new in EF but shouldn't I have this in my User class. If I need to get `context.Users.JobApplies.ToList();` if I remove that I can't do this. – 1110 Jun 29 '14 at 11:44
  • I didn't know you have JobApplies property. I've editted my answer, could you check it? – mr100 Jun 29 '14 at 12:00
  • Works perfect. Whole thing would be much easier if they make `Id` from `ApplicationUser` to be named `UserId` :) Tnx – 1110 Jun 29 '14 at 12:10
  • @1110 - why would they want to do that? Entity Framework's conventions are that it looks for primary keys to be named Id or ClassNameId (ie ApplicationUserId). The whole thing here is just because you don't like the name Id and want it to be something else. That's just user preference. – Erik Funkenbusch Jun 30 '14 at 05:12
  • You are right it's user preference. I was just kidding about what ms should name it :-) I simply like UseId. What would you name columns in my situation (pk & fk)? – 1110 Jun 30 '14 at 06:16