Query: Support Include/ThenInclude for navigation on derived type
See original GitHub issueIs 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:
- Created 8 years ago
- Reactions:18
- Comments:58 (31 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
fixed in 07afd7aa330da5b6d90d518da7375d8bbf676dfd
@smitpatel @anpete for option 2, did you try writing the lambda like this?