dotnet / dotnet/efcore

Error selecting nested json collections with query splitting on

Open
#32,502 1 comment 2 reactions 0 assignees View on GitHub
area-json area-query customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

Hi, everyone.

When I try to select nested JSON collection with the following config:
```C#
modelBuilder.Entity()
.OwnsMany(e => e.Inner, b => b.OwnsMany(e => e.Values, b2 => b2.ToTable(table)).ToJson("inner").ToTable(table))
.ToTable(table);
```
Query:
```C#
dbContext.Set().AsNoTracking().Select(c =>
c.Inner!.Where(c => c.Key == "Asd")
.Select(c => new NamedTimeSeriesEntry { Key = c.Key, Values = c.Values.Where(v => v.Date <= new DateTime(2022, 3, 1)).ToList() }))
.ToListAsync().Result;
```

When query splitting is on (`UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)`) I get the following exception:
```
System.AggregateException: 'One or more errors occurred. (The given key 'Property: TimeSeriesEntry.NamedTimeSeriesEntryId (no field, int) Shadow Required PK FK AfterSave:Throw' was not present in the dictionary.)'
```

When I turn query splitting off it works fine. It seems that query splitting should not be used for JSON entities.
Any workarounds for now?
Minimum repro included.
[Json-Nested-Collection-Query-minirepro.zip](https://github.com/dotnet/efcore/files/13542046/Json-Nested-Collection-Query-minirepro.zip)

EF Core version: 8.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer 8.0.0, Npgsql.EntityFrameworkCore.PostgreSQL 8.0.0
Target framework: .NET 8.0
Operating system: Windows

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.