Skip to content

EF Core and ProjectToType eager loader and is ignoring include #468

Closed
@kavatari

Description

@kavatari

Following the next example: when using ProjectToType on a queryable Mapster is eager loading the data "SomeOtherEntity" and ignores the "include take(1)".

class SomeEntity 
{
    private ICollection<SomeOtherEntity> MyList { get; set; }
}

class SomeOtherEntity
{
    private SomeEntity myEntity { get; set; }
}

class MyModel 
{
    private ICollection<SomeOtherEntity> MyList { get; set; }
}

class MyOtherModel
{
    private MyModel myEntity { get; set; }
}

await _dbContext.SomeEntity
    .AsNoTracking()
    .Where(x => x.IsActive == isActive)
    .Include(x => 
        x.SomeOtherEntity
            .OrderByDescending(p => p.Date)
            .Take(1))
    .ProjectToType<MyModel>()
    .ToListAsync(cancellationToken);

Mapping data this way: works fine!

var data = await _dbContext.SomeEntity
            .AsNoTracking()
            .Where(x => x.IsActive == isActive)
            .Include(x => 
                x.SomeOtherEntity
                    .OrderByDescending(p => p.Date)
                    .Take(1))
            .ToListAsync(cancellationToken);

        return data.Adapt<List<SomeModel>>();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions