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

Đang mở
#39,843 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
65/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
csharp, sql
Lĩnh vực
database

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
C#
Star
14.8k
Fork
3.5k
Merge trung bình
2 ngày 22 giờ
Pull request đã merge (30 ngày)
158

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của dotnet/efcore

Tất cả issue của dotnet/efcore

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.