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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:19:21+00:00 2026-06-18T03:19:21+00:00

Is there a way to decorate your POCO classes to automatically eager-load child entities

  • 0

Is there a way to decorate your POCO classes to automatically eager-load child entities without having to use Include() every time you load them?

Say I have a Class Car, with Complex-typed Properties for Wheels, Doors, Engine, Bumper, Windows, Exhaust, etc. And in my app I need to load my car from my DbContext 20 different places with different queries, etc. I don’t want to have to specify that I want to include all of the properties every time I want to load my car.

I want to say

List<Car> cars = db.Car
                .Where(x => c.Make == "Ford").ToList();

//NOT .Include(x => x.Wheels).Include(x => x.Doors).Include(x => x.Engine).Include(x => x.Bumper).Include(x => x.Windows)


foreach(Car car in cars)
{

//I don't want a null reference here.

String myString = car.**Bumper**.Title;
}

Can I somehow decorate my POCO class or in my OnModelCreating() or set a configuration in EF that will tell it to just load all the parts of my car when I load my car? I want to do this eagerly, so my understanding is that making my navigation properties virtual is out. I know NHibernate supports similar functionality.

Just wondering if I’m missing something. Thanks in advance!

Cheers,

Nathan

I like the solution below, but am wondering if I can nest the calls to the extension methods. For example, say I have a similar situation with Engine where it has many parts I don’t want to include everywhere. Can I do something like this? (I’ve not found a way for this to work yet). This way if later I find out that Engine needs FuelInjectors, I can add it only in the BuildEngine and not have to also add it in BuildCar. Also if I can nest the calls, how can I nest a call to a collection? Like to call BuildWheel() for each of my wheels from within my BuildCar()?

public static IQueryable<Car> BuildCar(this IQueryable<Car> query) {
     return query.Include(x => x.Wheels).BuildWheel()
                 .Include(x => x.Doors)
                 .Include(x => x.Engine).BuildEngine()
                 .Include(x => x.Bumper)
                 .Include(x => x.Windows);
}

public static IQueryable<Engine> BuildEngine(this IQueryable<Engine> query) {
     return query.Include(x => x.Pistons)
                 .Include(x => x.Cylendars);
}

//Or to handle a collection e.g.
 public static IQueryable<Wheel> BuildWheel(this IQueryable<Wheel> query) {
     return query.Include(x => x.Rim)
                 .Include(x => x.Tire);
}

Here is another very similar thread in case it is helpful to anyone else in this situation, but it still doesn’t handle being able to make nexted calls to the extension methods.

Entity framework linq query Include() multiple children entities

  • 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-18T03:19:23+00:00Added an answer on June 18, 2026 at 3:19 am

    No you cannot do that in mapping. Typical workaround is simple extension method:

    public static IQueryable<Car> BuildCar(this IQueryable<Car> query) {
         return query.Include(x => x.Wheels)
                     .Include(x => x.Doors)
                     .Include(x => x.Engine)
                     .Include(x => x.Bumper)
                     .Include(x => x.Windows);
    }
    

    Now every time you want to query Car with all relations you will just do:

    var query = from car in db.Cars.BuildCar()
                where car.Make == "Ford"
                select car;
    

    Edit:

    You cannot nest calls that way. Include works on the core entity you are working with – that entity defines shape of the query so after you call Include(x => Wheels) you are still working with IQueryable<Car> and you cannot call extension method for IQueryable<Engine>. You must again start with Car:

    public static IQueryable<Car> BuildCarWheels(this IQuerable<Car> query) {
        // This also answers how to eager load nested collections 
        // Btw. only Select is supported - you cannot use Where, OrderBy or anything else
        return query.Include(x => x.Wheels.Select(y => y.Rim))
                    .Include(x => x.Wheels.Select(y => y.Tire));
    }
    

    and you will use that method this way:

    public static IQueryable<Car> BuildCar(this IQueryable<Car> query) {
         return query.BuildCarWheels()
                     .Include(x => x.Doors)
                     .Include(x => x.Engine)
                     .Include(x => x.Bumper)
                     .Include(x => x.Windows);
    }
    

    The usage does not call Include(x => x.Wheels) because it should be added automatically when you request eager loading of its nested entities.

    Beware of complex queries produced by such complex eager loading structures. It may result in very poor performance and a lot of duplicate data transferred from the database.

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

Sidebar

Related Questions

Is there way to automatically maximize the output window on hitting build and then
Is there way to set @include mixin(); to variable? I tried this @mixin bg-gradient($fallback,
Is there a way to a coded class called Pessoa decorate some atttribute to
Is there some way to avoid this. I have a lot of classes that
Is there a way to decorate a function in Ruby as it's done in
Is there a way to decorate a method that will do some logging, then
Is there any way to manually decorate function names in MS C++? I'm going
Is there way to get file from windows xp command prompt? I tried to
Is there way to copy a file into Plone with WebDAV and have Plone
is there way how to get name ov event from Lambda expression like with

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.