question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Query: Support Include/ThenInclude for navigation on derived type

See original GitHub issue

Is there a solution for using ThenInclude with polymorphic types?

Example model:

public abstract class BasePosition : Entity
{
    public Order Order { get; set; }
}

public class GroupType : Entity
{}

public class GroupPosition : BasePosition
{
      public GroupType GroupType { get; set; }
}

public class SalesPosition : BasePosition
{
    public GroupPosition Group { get; set; }
}

public class Order : Entity
{
    private List<BasePosition> positions;

    public ICollection<BasePosition> Positions 
    => positions ?? (positions = new List<BasePosition>());
}

Example query:

context.Set<Order>()
.Include(x => x.Positions)
    .ThenInclude<SalesPosition>(x => x.Group) // Pseudo-Code, does obviously not work
    .ThenInclude<GroupPosition>(x => x.GroupType) // Pseudo-Code, does obviously not work

AFAIK the only way this currently works is by manually querying the derived types after loading the base shape:

var o = context.Set<Order>()
.Where(o => o.Id == 1234)
.Single();

context.Set<GroupPosition>()
    .Where(x => x.Order == o)
    .Include(x => x.GroupType)
    .Load();

context.Set<SalesPosition>()
    .Where(x => x.Order == o)
    .Include(x => x.Group)
    .Load();

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:18
  • Comments:58 (31 by maintainers)

github_iconTop GitHub Comments

7reactions
maumarcommented, Aug 19, 2017

fixed in 07afd7aa330da5b6d90d518da7375d8bbf676dfd

6reactions
divegacommented, Jun 9, 2017

@smitpatel @anpete for option 2, did you try writing the lambda like this?

queryOfBase.Include((Derived e) => e.NavigationOnDerived);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Eager Loading of Related Data - EF Core
You can include related data from navigation defined only on a derived type using Include and ThenInclude . Given the following model: C#...
Read more >
EF Core - Include ThenInclude Interface
There is an GitHub Issue Query: Support Include/ThenInclude for navigation on derived type requesting this feature, which was added to ...
Read more >
Include for derived types in Entity Framework 2.1
We can cast base type to derived type when calling Include() method and tell what navigation property we want to be included like...
Read more >
Querying data via the DbSet
In EF Core, you can use the Include method to load related data along with the main entity data in a single database...
Read more >
Entity Framework Core 5 - Pitfalls To Avoid and Ideas to Try
In this post, we'll look at some pitfalls and ideas EF Core users like yourself may want to consider when developing an application....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found