title: How can I load Child entities dynamically in Entity Framework 6
body: <p>I have a Provider class and an Article one. Article has a <code>int ProviderId{get;set}</code> and a <code>public virtual Provider Provider {get;set;}</code> properties.</p>
  
  <p>I know about Lazy loading and why I can't access the Provider property in Article outside the context but I have a generic method that returns the next T like this:</p>
  
  <pre><code>public static T Next&lt;T&gt;(T currentElement) where T : class, IModel {
      T data;
  
      if (currentElement.Id &gt;= GetLastId&lt;T&gt;())
          return currentElement;
  
      using (DatabaseEntities context = new DatabaseEntities()) {
          data = context.Set&lt;T&gt;().Single(el =&gt; el.Id == currentElement.Id + 1);
      }
  
      return data;
  }
  </code></pre>
  
  <p>But I can't retrieve Child entities like Provider in Articles Class. How can I include all entities? Should I update the method and make one per entity?</p>
  
  <p>I read about <a href="https://docs.microsoft.com/en-us/ef/core/querying/related-data#eager-loading" rel="nofollow noreferrer">Eager loading</a> and <a href="https://docs.microsoft.com/en-us/ef/core/querying/related-data#explicit-loading" rel="nofollow noreferrer">Explicit loading</a> but I don't know how can I implement these in my method.</p>
  
  <p><strong>Note:</strong></p>
  
  <p>Not all my entities have entity children and I have more methods like <code>Previous&lt;T&gt;()</code>, <code>First&lt;T&gt;()</code> or <code>Last&lt;T&gt;()</code> that do the work you expect.</p>