title: How to eagerly load all referenced entities dynamically in Entity Framework
body: <ul>
  <li>EF 6</li>
  <li>I have the following POCO's</li>
  </ul>
  
  <p>_</p>
  
  <pre><code>public class StructureEntity : EmEntityBase
  {
      [ForeignKey("ParentStructureId")]
      public virtual StructureEntity ParentStructure { get; set; }
  
      public long? ParentStructureId { get; set; }
  
      [ForeignKey("SiteId")]
      public virtual SiteEntity Site { get; set; }
  
      [Required(ErrorMessage = "Required.")]
      public long SiteId { get; set; }
  
      [Required(ErrorMessage = "Required.")]
      public string Name { get; set; }
  }
  
  public class SiteEntity : EmEntityBase
  {
      [ForeignKey("ParentSiteId")]
      public virtual SiteEntity ParentSite { get; set; }
  
      public long? ParentSiteId { get; set; }
  
      [Required(ErrorMessage = "Required.")]
      public long ClientId { get; set; }
  
      [ForeignKey("ClientId")]
      public ClientEntity Client { get; set; }
  
      [Required(ErrorMessage = "Required.")]
      public string Name { get; set; }
  }
  
  public class ClientEntity : EmEntityBase
  {
      public long? ParentClientId { get; set; }
  
      [ForeignKey("ParentClientId")]
      public virtual ClientEntity ParentClient { get; set; }
  
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>Now when I want to eagerly load all the referenced entities. To do this I have:</p>
  
  <pre><code>    public IQueryable&lt;StructureDivisionEntity&gt; GetAllWithInclude()
     {
     return GetAll()
           .Include(e =&gt; e.Structure)
           .Include(e =&gt; e.ParentStructureDivision.Structure)
           .Include(e =&gt; e.Structure.Site.Client);
     }
  </code></pre>
  
  <p>I was wondering if there was a dynamic way to do this without explicitly having to do the .Include(Structure) etc. Something along the lines of:</p>
  
  <p>MyEntity.IncludeAllReferenced() where IncludeAllReferenced used reflection or similair to traverse MyEntity and do all the includes?</p>
  
  <p><strong>UPDATE: Alternative Approach To Querying Complex Graphs</strong></p>
  
  <ul>
  <li><a href="http://www.codeproject.com/Articles/247254/Improving-Entity-Framework-Query-Performance-Using" rel="nofollow">http://www.codeproject.com/Articles/247254/Improving-Entity-Framework-Query-Performance-Using</a></li>
  <li><a href="https://entityframework.codeplex.com/workitem/1386" rel="nofollow">https://entityframework.codeplex.com/workitem/1386</a></li>
  <li><a href="https://github.com/oinuar/GBQ" rel="nofollow">https://github.com/oinuar/GBQ</a></li>
  </ul>