Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ThenInclude with Select unexpected behavior #32796

Closed
daniel18acevedo opened this issue Jan 12, 2024 · 2 comments
Closed

ThenInclude with Select unexpected behavior #32796

daniel18acevedo opened this issue Jan 12, 2024 · 2 comments

Comments

@daniel18acevedo
Copy link

Evidence

image

image

Repo

https://github.com/daniel18acevedo/efcore-then-include-select-issue

Include stack traces

Parent property is being null but Category gets included just fine

Include provider and version information

EF Core version: 7.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET 6.0
Operating system: Windows
IDE: Visual Studio 2022 17.4

@maumar
Copy link
Contributor

maumar commented Jan 17, 2024

this is query we get:

DbSet<Request>()
    .Include(r => r.Category)
    .ThenInclude(c => c.ParentCategory)
    .Where(r => r.Category.IsActive && r.Category.ParentCategoryId.HasValue)
    .GroupBy(r => r.Category)
    .Select(g => new ResultGroupByCategoryQuery{ 
        Category = g.Key, 
        Parent = g.Key.ParentCategory 
    }
    )

nav expansion converts it to:

DbSet<Request>()
    .Join(
        inner: DbSet<Category>(), 
        outerKeySelector: r => EF.Property<Guid?>(r, "CategoryId"), 
        innerKeySelector: c => EF.Property<Guid?>(c, "CategoryId"), 
        resultSelector: (o, i) => new TransparentIdentifier<Request, Category>(
            Outer = o, 
            Inner = i
        ))
    .Where(r => r.Inner.IsActive && r.Inner.ParentCategoryId.HasValue)
    .GroupBy(r => r.Inner)
    .Select(g => new ResultGroupByCategoryQuery{ 
        Category = g.Key, 
        Parent = g.Key.ParentCategory 
    }
    )

note that Parent = g.Key.ParentCategory has not been expanded

this results in the following translation:

ShapedQueryExpression: 
    QueryExpression: 
        Client Projections:
            0 -> Dictionary<IProperty, int> { [Property: Category.CategoryId (Guid) Required PK AfterSave:Throw ValueGenerated.OnAdd, 0], [Property: Category.IsActive (bool) Required, 1], [Property: Category.Name (string), 2], [Property: Category.ParentCategoryId (Guid?) FK Index, 3] }
        SELECT c.CategoryId, c.IsActive, c.Name, c.ParentCategoryId
        FROM Requests AS r
        INNER JOIN Categories AS c ON r.CategoryId == c.CategoryId
        WHERE c.IsActive && c.ParentCategoryId IS NOT NULL
        GROUP BY c.CategoryId, c.IsActive, c.Name, c.ParentCategoryId
    ShaperExpression: new ResultGroupByCategoryQuery{ 
            Category = StructuralTypeShaperExpression: 
                Category
                ValueBufferExpression: 
                    ProjectionBindingExpression: 0
                IsNullable: False
            , 
            Parent = StructuralTypeShaperExpression: 
                Category
                ValueBufferExpression: 
                    ProjectionBindingExpression: 0
                IsNullable: False
             == default(Category) ? null : StructuralTypeShaperExpression: 
                Category
                ValueBufferExpression: 
                    ProjectionBindingExpression: 0
                IsNullable: False
            .ParentCategory 
        }

we just project category entity and try to access ParentCategory on the client, which is obviously always null.

@maumar
Copy link
Contributor

maumar commented Jan 17, 2024

duplicate of #29014

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants