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

  • SEARCH
  • Home
  • 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 8619735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:24:42+00:00 2026-06-12T06:24:42+00:00

I have a pair of simple classes generating a database in Code First in

  • 0

I have a pair of simple classes generating a database in Code First in EF 4.1:

public class User
{
    public int UserId { get; set; }
    public string UserName { get; set; }
}

public class Purchase
{
    public int PurchaseId { get; set; }
    public int UserId { get; set; }
    public int? SalespersonUserId { get; set; }

    public User User { get; set; }
    public User SalespersonUser { get; set; }
}

public class NewItemsDataContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Purchase> Purchases { get; set; }
}

In my program, I create and write some data to it.

class Program
{
    static void Main(string[] args)
    {
        Database.SetInitializer<NewItemsDataContext>(new DropCreateDatabaseIfModelChanges<NewItemsDataContext>());

        using (NewItemsDataContext sadc = new NewItemsDataContext())
        {
            sadc.Users.Add(new User());

            sadc.SaveChanges();
        }
        using (NewItemsDataContext sadc = new NewItemsDataContext())
        {
            sadc.Purchases.Add(new Purchase() { UserId = 1 });
            sadc.SaveChanges();
        }
        using (NewItemsDataContext sadc = new NewItemsDataContext())
        {
            var sql = sadc.Purchases.Include(p => p.User);
            foreach (Purchase purchase in sql)
                Console.WriteLine(purchase.User.UserId.ToString());
        }
    }
}

Note, when I read the Purchase records back, I get an exception of purchase.User being null — that is, the .Include did not pull anything in for the User. Now, if I ignore the salespersonUser navigation property in my OnModelCreating (or just comment it out):

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Purchase>().Ignore(p => p.SalespersonUser);

    base.OnModelCreating(modelBuilder);
}

The code works, and the User is loaded in the .Include.

When the SalespersonUser nav property is ignored, the created database looks as you would expect it. The Purchase table has a PurchaseId, UserId, and SalespersonId. But once you add the SalespersonUser nav property back in (stop ignoring it), you end up with two more keys in the table: User_UserId and SalespersonUser_UserId (as well as the original UserId and SalespersonUserId).

Also, the SQL generated definitely shows where the problem arises.

Without the nav property:

{SELECT 
[Extent1].[PurchaseId] AS [PurchaseId], 
[Extent1].[UserId] AS [UserId], 
[Extent1].[SalespersonUserId] AS [SalespersonUserId], 
[Extent2].[UserId] AS [UserId1], 
[Extent2].[UserName] AS [UserName]
FROM  [Purchases] AS [Extent1]
INNER JOIN [Users] AS [Extent2] ON [Extent1].[UserId] = [Extent2].[UserId]}

and with the nav property:

{SELECT 
[Extent1].[PurchaseId] AS [PurchaseId], 
[Extent1].[UserId] AS [UserId], 
[Extent1].[SalespersonUserId] AS [SalespersonUserId], 
[Extent2].[UserId] AS [UserId1], 
[Extent2].[UserName] AS [UserName], 
[Extent1].[SalespersonUser_UserId] AS [SalespersonUser_UserId]
FROM  [Purchases] AS [Extent1]
LEFT OUTER JOIN [Users] AS [Extent2] ON [Extent1].[User_UserId] = [Extent2].[UserId]}

Notice how it pulls the UserId, but joins with the User_UserId, and a left join, no less. The SalespersonUserId isn’t even referenced in a join. Inside the database after writing the record, the UserId is set, but the User_UserID is null. Thus, nothing to join, and we get null for the Included User.

It would seem to me that this is a bug in EF, but it is possible there is a design reason for it. If so, can someone clear it up for me, and perhaps describe some fluent API that may work around it? I’m kind of partial to my nav properties.

  • 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-06-12T06:24:43+00:00Added an answer on June 12, 2026 at 6:24 am

    EF is having problems w/ your navigation properties – thats why it’s generating the extra FK’s.

    Try adding this fluent mapping, so you’r explicitly mapping the FK to nav property relationship:

            modelBuilder.Entity<Purchase>()
                .HasOptional(p => p.SalespersonUser)
                .WithMany()
                .HasForeignKey(p => p.SalespersonUserId);
    
            modelBuilder.Entity<Purchase>()
                .HasRequired(p => p.User)
                .WithMany()
                .HasForeignKey(p => p.UserId);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Custom Collection Initializers I have a simple Pair class: public class Pair<T1,
I have a simple pair of classes that looks like this: class X include
I have a public/private key pair set up so I can ssh to a
I have a simple class class symbol_entry { private: static unsigned long uid; public:
I have this simple client-server application pair. The code is pretty simple, I'm using
I currently have an array of pair<double, int> which I sort using a simple
I have vector< pair<int, int>> myVec (N); I want to have all pairs initialized
Given a set of tuple classes in an OOP language: Pair, Triple and Quad,
I have some code that looks like the following. First I have some domain
I have a written a simple Client-Server pair, sending an Object to the server.

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.