0

I have 3 types of Posts: PostA, PostB, PostC which all inherits from Post. I have one table for all of them defined in Context.cs like that: public DbSet<Post> Posts { get; set; }

Now I need to retrieve informations based on Id, but don't know how to query this with Includes.

Post post = _context.Posts
    .Include(p => p.AddedBy)
    .Include(p => p.Responses)
        .ThenInclude(res => res.User)
    .Include(p => p.Responses)
        .ThenInclude(res => res.Comments)

    //.OfType<PostA>()
    //    .Include(a => a.PostATags)
    //        .ThenInclude(r => r.Tag)
    //.OfType<PostB>()
    //    .Include(b => b.Article)

    .FirstOrDefault(p => p.Id == id);

I don't know the type when querying so I can't apply OfType<>() here. Above query works, but doesn't include all data i need.

Piotrek
  • 1,304
  • 8
  • 16

1 Answers1

0

I achieved it like that:

Post post = _context.Posts
    .Include(p => p.AddedBy)
    .Include(p=> p.Responses)
        .ThenInclude(res => res.User)
    .Include(p => p.Responses)
        .ThenInclude(res => res.Comments)

    .Include(p => (p as PostA).PostATags)
        .ThenInclude(r => r.Tag)
    .Include(p => (p as PostB).Article)

    .FirstOrDefault(p => p.Id == id);
Piotrek
  • 1,304
  • 8
  • 16