title: Entity framework - Conditionally query related entities with derived types methods
body: <p>I have three class:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public abstract class Conversation
  {
      public int Id { get; set; }
  
      public abstract bool HasUser(string userId);
  }
  
  public class PublicConversation : Conversation
  {
      public override bool HasUser(string userId)
      {
          return true;
      }
  }
  
  public class PrivateConversation : Conversation
  {
      public ICollection&lt;User&gt; Users { get; set; }
  
      public override bool HasUser(string userId)
      {
          return Users.Any(t =&gt; t.UserId == userId);
      }
  }
  </code></pre>
  
  <p>In DbContext, there are DbSets:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public DbSet&lt;Conversation&gt; Conversations { get; set; }
  public DbSet&lt;PrivateConversation&gt; PrivateConversations { get; set; }
  public DbSet&lt;PublicConversation&gt; PublicConversations { get; set; }
  
  public DbSet&lt;User&gt; Users { get; set; }
  </code></pre>
  
  <p>Which generates two tables: <code>Conversations</code> and <code>Users</code>.</p>
  
  <blockquote>
    <p>PrivateConversations and PublicConversations are saved in table <code>Conversations</code></p>
  </blockquote>
  
  <p>Now an error occurred with query:</p>
  
  <pre class="lang-cs prettyprint-override"><code>
  // For some reason, I can only query `db.Conversations`, can not access `db.PrivateConversations`
  var conversations = db.Conversations
    .Include(t =&gt; (t as PrivateConversation).Users)
    .Where(t =&gt; t.HasUser("something"))
    .ToList();
  </code></pre>
  
  <p>For all <code>PublicConversation</code>, everything works fine.</p>
  
  <p>Told me that in <code>PrivateConversation</code> method: <code>HasUser()</code>, <code>Users.Any()</code>, <code>Users</code> is null.</p>
  
  <p><a href="https://i.stack.imgur.com/MPsyZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MPsyZ.png" alt="enter image description here"></a></p>
  
  <p>I am very confused. How can I query all private conversations with user in it?</p>
  
  <h3>Additional info:</h3>
  
  <p>packages I usesd:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.6" /&gt;
  &lt;PackageReference Include="Microsoft.EntityFrameworkCore.SQLServer" Version="2.2.6" /&gt;
  </code></pre>
  
  <p>runtime: .NET Core 2.2</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;OutputType&gt;Exe&lt;/OutputType&gt;
  &lt;TargetFramework&gt;netcoreapp2.2&lt;/TargetFramework&gt;
  </code></pre>