efcore icon indicating copy to clipboard operation
efcore copied to clipboard

How to include a property of a subclass

Open mkucera47 opened this issue 10 years ago • 4 comments

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?

mkucera47 avatar Dec 17 '15 21:12 mkucera47

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.

techyian avatar Dec 23 '15 16:12 techyian

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).

rowanmiller avatar Dec 23 '15 21:12 rowanmiller

Confirmed that this is currently not possible and we have #3910 tracking enabling this feature.

rowanmiller avatar Dec 28 '15 19:12 rowanmiller

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!

gelanquizora avatar May 08 '23 02:05 gelanquizora