Home > Software design >  EntityFrameworkCore 5.0.9 Extension Method with eager loading Include does not work without Load();
EntityFrameworkCore 5.0.9 Extension Method with eager loading Include does not work without Load();

Time:09-16

I have the following code:

public static class IntegrationServiceExtensions
{
    public static IQueryable<T> LearningObjectContainerIncludeNestedProperties<T>(this IQueryable<T> query) where T : LearningObjectContainer
    {
        query.Include(x => x.Bases)
            .Include(x => x.LearningObjectives).ThenInclude(x => x.Classifications)
            .Include(x => x.Classifications)
            .Include(x => x.ContainerPropertyImplementations);

        return query;
    }
}

//Include does not work
public async Task<LearningObjectContainer> GetLearningObjectContainer(int id)
{
    var query = _applicationDbContext.LearningObjectContainers.LearningObjectContainerIncludeNestedProperties();

    var learningObjectContainer = await query.FirstOrDefaultAsync(x => x.Id == id);

    return learningObjectContainer;
}

//Include works
public async Task<LearningObjectContainer> GetLearningObjectContainer(int id)
{
    var query = _applicationDbContext.LearningObjectContainers
        .Include(x => x.Bases)
        .Include(x => x.LearningObjectives).ThenInclude(x => x.Classifications)
        .Include(x => x.Classifications)
        .Include(x => x.ContainerPropertyImplementations);

    var learningObjectContainer = await query.FirstOrDefaultAsync(x => x.Id == id);

    return learningObjectContainer;
}

enter image description here

enter image description here

Working:

enter image description here

enter image description here

It does not matter if I run the code asynchronous or synchronous.

The only way I made the extension method work is by using .Load(); (explicit loading).

enter image description here

Why can I not use eager loading when using an extension method?

https://docs.microsoft.com/en-us/ef/core/querying/related-data/eager

I tried to use split queries with eager loading but it did not work.

public static IQueryable<T> LearningObjectContainerIncludeNestedProperties<T>(this IQueryable<T> query) where T : LearningObjectContainer
{
    query.Include(x => x.Bases)
        .Include(x => x.LearningObjectives).ThenInclude(x => x.Classifications)
        .Include(x => x.Classifications)
        .Include(x => x.ContainerPropertyImplementations)
        .AsSplitQuery();

    return query;
}

https://docs.microsoft.com/en-us/ef/core/querying/single-split-queries

CodePudding user response:

I think this method should be:

public static IQueryable<T> LearningObjectContainerIncludeNestedProperties<T>(this IQueryable<T> query) where T : LearningObjectContainer
{
    query = query.Include(x => x.Bases)
        .Include(x => x.LearningObjectives).ThenInclude(x => x.Classifications)
        .Include(x => x.Classifications)
        .Include(x => x.ContainerPropertyImplementations);

    return query;
}

It was just missing the query re-assignment to the IQueryable result /w Include. ("query = ")

  • Related