I have three class:
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<User> Users { get; set; }
public override bool HasUser(string userId)
{
return Users.Any(t => t.UserId == userId);
}
}
In DbContext, there are DbSets:
public DbSet<Conversation> Conversations { get; set; }
public DbSet<PrivateConversation> PrivateConversations { get; set; }
public DbSet<PublicConversation> PublicConversations { get; set; }
public DbSet<User> Users { get; set; }
Which generates two tables: Conversations
and Users
.
PrivateConversations and PublicConversations are saved in table
Conversations
Now an error occurred with query:
// For some reason, I can only query `db.Conversations`, can not access `db.PrivateConversations`
var conversations = db.Conversations
.Include(t => (t as PrivateConversation).Users)
.Where(t => t.HasUser("something"))
.ToList();
For all PublicConversation
, everything works fine.
Told me that in PrivateConversation
method: HasUser()
, Users.Any()
, Users
is null.
I am very confused. How can I query all private conversations with user in it?
Additional info:
packages I usesd:
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SQLServer" Version="2.2.6" />
runtime: .NET Core 2.2
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>