How to include nested EF entities in include
See original GitHub issueDescription
I’m using AutoMapper as I expose through a Resource
information that I take from several classes that require EF navigation. See src.EntityTypeNameType.NameType.Code
in the mapping that I use…
CreateMap<Model.EntityName, DTO.EntityName>()
.ForMember(dest => dest.NameType,
opts => opts.MapFrom(src => src.EntityTypeNameType.NameType.Code));
What happens is that with the DefaultEntityRepository
I see only the base include is included unless I force it.
My idea is to override the IQueryable<Entity> Include(...)
and create a Dictionary of the nested includes required that need to be loaded to query all the tables in the relationship.
This is more a design question or feature-request rather than a bug. Does it make sense the solution I’m planning?
btw, congrats for this project looks very nice! 😉
Environment
- JsonApiDotNetCore Version: v3.1.0
- Other Relevant Package Versions: AutoMapper
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:13 (11 by maintainers)
Top Results From Across the Web
EF LINQ include multiple and nested entities
In Entity Framework Core ( EF.core ) you can use .ThenInclude for including next levels. var blogs = context.Blogs .Include(blog => blog.
Read more >Question about eager loading of nested entities using ...
I have a problem regarding loading a deeply nested hierarchy of entities. I have 2 entities for distinct set of attributes.
Read more >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 >Include in EF Core 2.0 create infinite nested child Entities ...
When I use Include method in a entity that have a One-to-one relationship with another one, I get a infinite nested structure.
Read more >Entity-framework – Include nested entities using LINQ
I'm messing around with LINQ for the first time, and I'm using EF 4.1 code first. I have entities containing nested Lists of...
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
The problem here is that these dependencies cross resource-specific services and repositories, so it cannot be solved in a good way at those levels.
Let me illustrate using an example. Consider the next models:
When getting users, it fails with a
NullReferenceException
inDisplayName
because the Name navigation property was not loaded. In this case, it must be loaded unconditionally. One solution, at first sight, would be to overrideDefaultResourceRepository.Get()
:That works for the /users endpoint. But getting /blogs?include=author will cause the
NullReferenceException
again. So now we need a conditional include in the blogs repository:I know there are other places we can do the inclusion, like the
DefaultResourceRepository.Include()
method orDefaultResourceService.ApplyInclude()
, but that does not change my point. The problem is we need to duplicate the inclusion logic whenever a resource contains a User resource, or when a resource contains another resource that contains a User resource, etc.The best way I think to deal with this is to indicate on the resource itself which EF navigation properties must be unconditionally loaded. And then have the framework dynamically add EF includes for them.
Something like this (but with a better name):
I will get back at this tomorrow.