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.

How to include nested EF entities in include

See original GitHub issue

Description

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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
bart-degreedcommented, Feb 12, 2020

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:

public class User : Identifiable
{
    [Attr(isImmutable: true)] 
    [NotMapped] 
    public string DisplayName => Name.First + " " + Name.Last;

    public Name Name { get; set; }
}

public class Name // not exposed as resource, only database table
{
    public string First { get; set; }
    public string Last { get; set; }
}

public class Blog : Identifiable
{
    [HasOne]
    public User Author { get; set; }
}

When getting users, it fails with a NullReferenceException in DisplayName because the Name navigation property was not loaded. In this case, it must be loaded unconditionally. One solution, at first sight, would be to override DefaultResourceRepository.Get():

public class UserRepository : DefaultResourceRepository<User>
{
    public override IQueryable<User> Get() => base.Get().Include(user => user.Name);
}

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:

public class BlogRepository : DefaultResourceRepository<Blog>
{
    public override IQueryable<Blog> Get()
    {
        if (QueryStringHasAuthorIncluded())
        {
            return base.Get().Include(blog => blog.Author.Name);
        }
        else
        {
            return base.Get();
        }
    }
}

I know there are other places we can do the inclusion, like the DefaultResourceRepository.Include() method or DefaultResourceService.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):

public class User : Identifiable
{
    [Attr(isImmutable: true)] 
    [NotMapped] 
    public string DisplayName => Name.First + " " + Name.Last;

    [JsonApiAlwaysAutoLoad] // <-------
    public Name Name { get; set; }
}
1reaction
maureicommented, Feb 13, 2020

I will get back at this tomorrow.

Read more comments on GitHub >

github_iconTop 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 >

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