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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:52:39+00:00 2026-05-17T20:52:39+00:00

I have 2 Entity Framework entity sets with many-to-many relationship (compromised of 3 DB

  • 0

I have 2 Entity Framework entity sets with many-to-many relationship (compromised of 3 DB tables). For the sake of the example let’s say I have a Worker entity and a Company entity, where each worker can work at multiple companies and each company can have many workers.

I want to retrieve for each worker all the companies that he/she works for. The straightforward way would be to create a query for each worker that will fetch the companies using a join between the association table and the companies table, But this results in a round trip to the DB for each worker.

I am sure this can be done in a better more optimized way. Any help will be appreciated.

Thank you.

  • 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-17T20:52:40+00:00Added an answer on May 17, 2026 at 8:52 pm

    If your joining table doesn’t have any extra info (just the Id’s of Worker and Company), you should have only two entities in your model: Worker and Company. If EF 4 the entity graph is eager loaded by default, so unless you enable LazyLoading by doing (context.ContextOptions.LazyLoadingEnabled = true;), you get your company lists whenever your query for the workers:

    var workers = context.Workers.ToList();
    // Companies already loaded - do something with them
    var companiesForWorker0 = workers[0].Companies;  // Don't forget to check 
    ...                                              // for null in real code
    

    You can also directly tell EF to eager load the companies when querying for workers (that would be necessary if LazyLoading is enabled):

    var workers = context.Workers.Include("Companies").ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Entity Framework objects with a one-to-many relationship: widget(parent) and widgetNail(child) Widget
I have an entity framework project setup. It it I have four tables and
I have a problem with the following Linq query using Entity Framework: from o
I am using Entity Framework with my website. To improve performance, I have started
Setting up ASP.net MVC with Linq2SQL or Entity Framework's context to have scaffolding work
So, I am using the Linq entity framework. I have 2 entities: Content and
Lets say I have an entity Foo, and it contains a list of another
I have an app using the ADO.NET entity framework (the VS2008 version, not the
I have Entity Framework entities Events which have an EntityCollection of RSVP. I want
I am using entity framework with a web service and I have entity partial

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.