Skip to content

Fix navigation property extraction for TPC inheritance in batch paging#8906

Merged
michaelstaib merged 3 commits into
ChilliCream:mainfrom
PHILLIPS71:jp/fix-tpc-navigation-property-extraction
Nov 16, 2025
Merged

Fix navigation property extraction for TPC inheritance in batch paging#8906
michaelstaib merged 3 commits into
ChilliCream:mainfrom
PHILLIPS71:jp/fix-tpc-navigation-property-extraction

Conversation

@PHILLIPS71
Copy link
Copy Markdown
Contributor

@PHILLIPS71 PHILLIPS71 commented Nov 15, 2025

Summary

Fixes navigation property extraction for TPC (Table Per Concrete) inheritance when using batch paging with selectors, while maintaining backward compatibility for scalar property grouping.

Problem

When using TPC inheritance mapping with QueryContext selectors and ToBatchPageAsync, queries would fail with an EF Core translation error:

await database
    .Entries
    .AsNoTracking()
    .Where(x => x.ParentId.HasValue && keys.Contains(x.ParentId.Value))
    .With(query, x => x.AddAscending(y => y.Id))
    .ToBatchPageAsync(x => x.ParentId!.Value, paging, cancellation);
The LINQ expression 'DbSet<FileSystemEntry>()
    .Where(f => f.ParentId.HasValue && __keys_0
        .Contains(f.ParentId.Value))
    .GroupBy(f => (f is FileSystemDirectory) ? (FileSystemEntry)new FileSystemDirectory{ Id = ((FileSystemDirectory)f).Id }
     : (f is FileSystemFile) ? (FileSystemEntry)new FileSystemFile{ Id = ((FileSystemFile)f).Id }
     : null.ParentId.Value)' could not be translated.

Root Cause

The EnsureGroupPropsAreSelected method was extracting the entire member expression chain (e.g., e.Parent.Id) instead of just the navigation property (e.g., e.Parent). When TPC inheritance is used with selectors, EF Core needs navigation properties to be explicitly included in the projection, but the incorrect extraction meant:

  1. The navigation property wasn't being added to the selector
  2. The grouping tried to access .Parent.Id on the projected instance
  3. EF Core couldn't translate the resulting expression

Example scenario that failed:

var query = new QueryContext<FileSystemEntry>(
  Selector: e => new FileSystemDirectory { Id = e.Id, Name = e.Name });

await context
  .Entries
  .With(query)
  .ToBatchPageAsync(
      keySelector: e => e.Parent!.Id,  // failed - navigation property not in selector
      arguments: pagingArgs);

Solution

Adjusted the navigation property extraction logic in GetMemberExpressions to:

  1. Traverse the member expression chain from leaf to root
  2. Extract intermediate navigation properties (e.g., e.Parent from e.Parent.Id)
  3. Extract root-level scalar properties for backward compatibility (e.g., e.BrandId)
  4. Handle nullable scalar properties (e.g., e.ParentId from e.ParentId.Value)

What's extracted:

  • e => e.Parent.Id → extracts e.Parent (navigation property)
  • e => e.BrandId → extracts e.BrandId (scalar foreign key)
  • e => e.ParentId.Value → extracts e.ParentId (nullable scalar)

@michaelstaib michaelstaib self-requested a review November 16, 2025 10:10
@michaelstaib michaelstaib merged commit 7f5b3d0 into ChilliCream:main Nov 16, 2025
112 of 113 checks passed
@codecov
Copy link
Copy Markdown

codecov Bot commented Nov 16, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 0.00%. Comparing base (7f3d649) to head (e294fc4).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@     Coverage Diff      @@
##   main   #8906   +/-   ##
============================
============================

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants