title: It is possible to use child class to implement Separation of concerns using EF Core?
body: <p>My goal is async loading of related entities using DBContext.</p>
  
  <p>Let imagine two projects. The first named <code>MyApp.Domain</code> and contains domain entities.</p>
  
  <pre class="lang-cs prettyprint-override"><code>namespace MyApp.Domain
  {
      public class PlanPage
      {
          public Guid Id { get; set; }
      }
  }
  </code></pre>
  
  <pre class="lang-cs prettyprint-override"><code>namespace MyApp.Domain
  {
      public class PlanPageDay
      {
          public Guid Id { get; set; }
          public Guid PlanPageId { get; set; }
      }
  }
  </code></pre>
  
  <p>The second project named <code>MyApp.Infrastructure.EntityFramework</code> and contains configuration of projection entities to database. It also contains class which extends domain entity and implements Entity framework specific logic.</p>
  
  <pre class="lang-cs prettyprint-override"><code>namespace MyApp.Infrastructure.EntityFramework.Models
  {
      public class PlanPageEntity : PlanPage
      {
          private readonly ApplicationDbContext _applicationDbContext;
  
          protected PlanPageEntity(ApplicationDbContext applicationDbContext)
          {
              _applicationDbContext = applicationDbContext;
          }
  
          public ICollection&lt;PlanPageDay&gt;? Days { get; set; }
  
          public async Task&lt;ICollection&lt;PlanPageDay&gt;&gt; GetDays()
          {
              return Days ??= await _applicationDbContext.PlanPageDays
                  .Where(pd =&gt; pd.PlanPageId == Id)
                  .ToListAsync();
          }
      }
  }
  </code></pre>
  
  <p>The purpose of this example is simple. We separate infrastructure code from domain code. Look how do we plan to use this concept:</p>
  
  <pre class="lang-cs prettyprint-override"><code>// Entity initializing code. Placing somewhere in domain logic.
  var plan = new PlanPage(/*some constructor arguments*/);
  
  // Entity loading code. Placing somewhere in infrastructure implementation.
  public async Task&lt;PlanPage&gt; GetPlanPage(Guid id)
  {
      return await _applicationDbContext.Set&lt;PlanPageEntity&gt;().FindAsync(id);
  }
  </code></pre>
  
  <p>Note that we tell to Entity framework to use child class (<code>PlanPageEntity</code>) so it can handle all specific things that it can.</p>
  
  <p>The question is: Is it possible to configure the EF so that it allows us to use this concept?</p>