Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5850485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:08:50+00:00 2026-05-22T13:08:50+00:00

I’m playing around with EF Code First and need some help. A User can

  • 0

I’m playing around with EF Code First and need some help.

A User can create many Posts.
A User can recommend many Posts
A Post can have many user Recommendations.

Given this code:

public class User
{
    public int UserID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Post> Posts { get; set; }
    public virtual ICollection<Post> Recommendations { get; set; }
}


public class Post
{
    public int PostID { get; set; }
    public int UserID { get; set; }
    public virtual User User { get; set; }
    public virtual ICollection<User> RecommendingUsers { get; set; }
}

modelBuilder.Entity<Post>()
.HasMany(x => x.RecommendingUsers)
.WithMany(x => x.Recommendations)
.Map(x => x.MapLeftKey("PostID")
    .MapRightKey("UserID")
    .ToTable("Recommendations"));

I end up with an extra “User_UserID” column in the Post table, which it’s using for the foreign key:

PostID (PK, int, not null)
UserID (int, not null)
User_UserID(FK, int, null)

Why isn’t it using UserID as the foreign key?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T13:08:51+00:00Added an answer on May 22, 2026 at 1:08 pm

    I had the same issue and found two things:

    1. The conventions it’s using to map the column names are names like User_UserID and User_ID, not UserID. Solutions:
      1. You can vary the conventions it’s using. I’m not clear on whether this is possible in EF 4.1 – Scott Gu has a post lamenting that it is not, but I can see a Conventions property hanging off of the ModelBuilder class… with only one method… Add().
      2. You can set the foreign key name explicitly (see below).
    2. I had cases where I had actually fixed it per suggestions on StackOverflow but the cached DbModel meant it still appeared broken until I actually closed Cassini and forced the dev webserver to restart entirely.

    Unfortunately because you’re not using the naming conventions the framework assumes, you’re going to have to keep using the ForeignKey attribute all over your classes in order to get everything to tie together correctly, or it will keep mapping to the wrong column name.

    Setting the Foreign Key name looks like:

    [ForeignKey("UserID")]
    public virtual User User { get; set; }
    

    This explicitly tells the framework to use UserID instead of User_UserID

    In Many-to-Many cases, you need to both use the above Map call, and apply the ForeignKey attribute on both sides.

    This solved it for me.

    I see now why Scott lamented leaving Conventions out – this really burns when you’re trying to put things together on an existing database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.