[Bug] EF Core InvalidOperationException on split query with FromSql join and collection Include

Aperta
#39,843 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
65/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
csharp, sql
Ambito
database

Direzione di ricerca

Start from the provided EF Core query and reproduce the InvalidOperationException in a test or small console sample using EF Core 10.0.10. Trace the relational query translation for FromSql, Join, Include, and AsSplitQuery() through the relational query components. The work is done when the split query no longer throws for the collection include and returns the expected entities with ORDetails loaded.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

customer-reported
Bug description
Bug Summary

An unhandled System.InvalidOperationException occurs in Entity Framework Core when querying OR with OR.ORDetails included (AsSplitQuery()) via a FromSql join on @orIds.

Steps to Reproduce / Context
  • FE Screen / User Action: [Insert FE screen or action that triggers this request]
  • API Endpoint: [Insert endpoint, e.g., POST /api/...]
  • Request Payload / Parameters: [Insert sample payload, e.g., orIds, ClientId, WarehouseId]
Query Causing the Error
DbSet<JoinKey<long>>().FromSql(SELECT [Id] FROM @orIds, @p)
    .Join(
        inner: DbSet<OR>()
            .IgnoreQueryFilters(), 
        outerKeySelector: e => e.Id, 
        innerKeySelector: e => e.ORId, 
        resultSelector: (_, e) => e)
    .Where(e => (int?)e.ClientId == @_tenantInfo_ClientId)
    .Where(e => (int?)e.WarehouseId == @_tenantInfo_WarehouseId)
    .Include(x => x.ORDetails)
    .AsSplitQuery()

### Your code

```csharp
DbSet<JoinKey<long>>().FromSql(SELECT [Id] FROM @orIds, @p)
    .Join(
        inner: DbSet<OR>()
            .IgnoreQueryFilters(), 
        outerKeySelector: e => e.Id, 
        innerKeySelector: e => e.ORId, 
        resultSelector: (_, e) => e)
    .Where(e => (int?)e.ClientId == @_tenantInfo_ClientId)
    .Where(e => (int?)e.WarehouseId == @_tenantInfo_WarehouseId)
    .Include(x => x.ORDetails)
    .AsSplitQuery()
Stack traces
Microsoft.EntityFrameworkCore.Query: Debug: Including navigation: 'OR.ORDetails'.
Exception thrown: 'System.InvalidOperationException' in Microsoft.EntityFrameworkCore.Relational.dll
'Unable to translate a collection subquery in a projection since either parent or the subquery doesn't project necessary information required to uniquely identify it and correctly generate results on the client side. This can happen when trying to correlate on keyless entity type. This can also happen for some cases of projection before 'Distinct' or some shapes of grouping key in case of 'GroupBy'. These should either contain all key properties of the entity that the operation is applied on, or ...'
The thread '.NET TP Worker' (83024) has exited with code 0 (0x0).
Verbose output

EF Core version

10.0.10

Database provider

No response

Target framework

NET 10

Operating system

No response

IDE

No response

Lingua principale
C#
Stelle
14.8k
Fork
3.5k
Merge medio
2g 22h
PR unite (30g)
158

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di dotnet/efcore

Tutte le issue di dotnet/efcore

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.