How to include a property of a subclass
I have the following model:
class Image
{
public int Id { get; set; }
public string Url { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}
class ImagePost : BasePost
{
public List<Image> Images { get; set; }
}
class BasePost
{
public int Id { get; set; }
public string Heading { get; set; }
public string Text { get; set; }
}
class BloggingContext : DbContext
{
public DbSet<Image> Igames { get; set; }
public DbSet<BasePost> Posts { get; set; }
public DbSet<ImagePost> ImagePosts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Filename=blogs.db");
}
}
How can I call Include on db.Posts so that every ImagePost gets its Images property included?
What is the relationship between BasePost and ImagePost? From your model I can't see one. You will need to configure a relationship between these entities, therefore allowing you to call upon ImagePost.
Probably a dupe of https://github.com/aspnet/EntityFramework/issues/3910. I don't think there is any way to do this at the moment, though it would be good to support something like context.Posts.Include(p => (p as ImagePost).Images).
Confirmed that this is currently not possible and we have #3910 tracking enabling this feature.
Confirmed that this is currently not possible and we have #3910 tracking enabling this feature.
Probably a dupe of #3910. I don't think there is any way to do this at the moment, though it would be good to support something like
context.Posts.Include(p => (p as ImagePost).Images).
it's now 2023 and It's working! Thanks @rowanmiller!