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

オープン
#39,843 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
65/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
csharp, sql
領域
database

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
C#
スター
14.8k
フォーク
3.5k
平均マージ
2日 22時間
マージ済み PR(30日)
158

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

dotnet/efcore のほかの issue

dotnet/efcore の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。