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 8732727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:29:22+00:00 2026-06-13T09:29:22+00:00

So let’s assume I have the following entities and their context configuration set up

  • 0

So let’s assume I have the following entities and their context configuration set up as follows. I have omitted a lot of properties for brevity’s sake:

public class Company {
    public int Id { get; set; }
    public Location Location { get; set; }
}

public class Customer {
    public int Id { get; set; }
    public Location Location { get; set; }
}

public class Location {
    public int Id { get; set; }
}

public sealed class EntityDefaultContext : DbContext {
    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        modelBuilder.Entity<Company>().HasKey(m => m.Id).ToTable("Company");
        modelBuilder.Entity<Company>().Property(m => m.Id).HasColumnName("Id");
        modelBuilder.Entity<Company>().HasRequired(m => m.Location).WithRequiredDependent().Map(m => m.MapKey("LocationId"));

        modelBuilder.Entity<Customer>().HasKey(m => m.Id).ToTable("Customer");
        modelBuilder.Entity<Customer>().Property(m => m.Id).HasColumnName("Id");
        modelBuilder.Entity<Customer>().HasRequired(m => m.Location).WithRequiredDependent().Map(m => m.MapKey("LocationId"));

        modelBuilder.Entity<Location>().HasKey(m => m.Id).ToTable("Location");
        modelBuilder.Entity<Location>().Property(m => m.Id).HasColumnName("Id");
    }
}

So as you can see, both the Company and Customer entities hold a reference to the Location entity. Something that would normally be expected I believe.

I set up my DB context for just that as you can also see. But the SQL that EF generates is terribly inefficient:

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[LocationId] AS [LocationId], 
    [Extent3].[Id] AS [Id1]
FROM
    [dbo].[Customer] AS [Extent1]
LEFT OUTER JOIN [dbo].[Company] AS [Extent2] ON [Extent1].[LocationId] = [Extent2].[LocationId]
LEFT OUTER JOIN [dbo].[Company] AS [Extent3] ON [Extent1].[LocationId] = [Extent3].[LocationId]
LEFT OUTER JOIN [dbo].[Company] AS [Extent4] ON [Extent1].[LocationId] = [Extent4].[LocationId]

This is generated when I do something like this:

var q = from c in defaultContext.Set<Customer>().Include(m => m.Location)
        select c;

I am doing it like this for reasons that are not relevant to the question. The weird thing is that here is the SQL if I only configure the Location entity to be associated by only the Customer entity:

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[LocationId] AS [LocationId]
FROM
    [dbo].[Customer] AS [Extent1]
INNER JOIN [dbo].[Location] AS [Extent2] ON [Extent1].[LocationId] = [Extent2].[Id]

Which is what I would expect. This makes me think. Does EF not support this scenario? How could it not?

Thanks in advance.

  • 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-13T09:29:24+00:00Added an answer on June 13, 2026 at 9:29 am

    Your original mapping uses one-to-one relation. That always causes some special behavior. Moreover it has some other requirements to work correctly. What you probably want is one-to-many relation between Customer and Location and Company and Location. Change WithRequiredDependent to WithMany and it should work.

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

Sidebar

Related Questions

Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say that I have a date in R and it's formatted as follows.
Let's say I have some text as follows: do this, do that, then this,
let's say I have the following string: string s = A B C D
Let's see if I make myself clear. I have an old set of scripts
Let's say I have the following entity: public class Store { public List<Product> Products
let's say I have a select list as follows: <select name='languages'> <option value='german'>German</option> <option
Let's say I have the following classes : public class MyProductCode { private String
Let assume we have two activities. A - main activity, that is home launcher
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>

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.