title: Entity Framework - Is there a way to automatically eager-load child entities without Include()?
body: <p>Is there a way to decorate your POCO classes to automatically eager-load child entities without having to use <code>Include()</code> every time you load them? </p>
  
  <p>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. </p>
  
  <p>I want to say </p>
  
  <pre><code>List&lt;Car&gt; cars = db.Car
                  .Where(x =&gt; c.Make == "Ford").ToList();
  
  //NOT .Include(x =&gt; x.Wheels).Include(x =&gt; x.Doors).Include(x =&gt; x.Engine).Include(x =&gt; x.Bumper).Include(x =&gt; x.Windows)
  
  
  foreach(Car car in cars)
  {
  
  //I don't want a null reference here.
  
  String myString = car.**Bumper**.Title;
  }
  </code></pre>
  
  <p>Can I somehow decorate my POCO class or in my <code>OnModelCreating()</code> 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. </p>
  
  <p>Just wondering if I'm missing something. Thanks in advance!</p>
  
  <p>Cheers,</p>
  
  <p>Nathan</p>
  
  <p>I like the solution below, but <strong>am wondering if I can nest the calls to the extension methods.</strong> 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. <strong>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()?</strong></p>
  
  <pre><code>public static IQueryable&lt;Car&gt; BuildCar(this IQueryable&lt;Car&gt; query) {
       return query.Include(x =&gt; x.Wheels).BuildWheel()
                   .Include(x =&gt; x.Doors)
                   .Include(x =&gt; x.Engine).BuildEngine()
                   .Include(x =&gt; x.Bumper)
                   .Include(x =&gt; x.Windows);
  }
  
  public static IQueryable&lt;Engine&gt; BuildEngine(this IQueryable&lt;Engine&gt; query) {
       return query.Include(x =&gt; x.Pistons)
                   .Include(x =&gt; x.Cylendars);
  }
  
  //Or to handle a collection e.g.
   public static IQueryable&lt;Wheel&gt; BuildWheel(this IQueryable&lt;Wheel&gt; query) {
       return query.Include(x =&gt; x.Rim)
                   .Include(x =&gt; x.Tire);
  }
  </code></pre>
  
  <p>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.</p>
  
  <p><a href="https://stackoverflow.com/questions/3356541/entity-framework-linq-query-include-multiple-children-entities">Entity framework linq query Include() multiple children entities</a></p>