JSON mapping via complex types is significantly slower than via owned entities for nested hierachies
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
### Bug description
The documentation recommends switching to complex types: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-10.0/whatsnew#complex-and-owned-entity-types
Yet, I observed significantly slower insert performance and greatly increased memory allocations for more nested hierachies. This mainly comes from `DbSet.Add` but `DbContext.SaveChangesAsync` is slower too.
I threw together a simple benchmark (see attached project) and got the following results:
```
BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.8655/25H2/2025Update/HudsonValley2)
AMD Ryzen 7 5700X3D 3.00GHz, 1 CPU, 16 logical and 8 physical cores
.NET SDK 10.0.301
[Host] : .NET 10.0.9 (10.0.9, 10.0.926.27113), X64 RyuJIT x86-64-v3
Job-CNUJVU : .NET 10.0.9 (10.0.9, 10.0.926.27113), X64 RyuJIT x86-64-v3
InvocationCount=1 UnrollFactor=1
| Method | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen0 | Gen1 | Allocated | Alloc Ratio |
|-------------------------- |------------:|-----------:|-----------:|------------:|-------:|--------:|------------:|-----------:|-----------:|------------:|
| AddAndSaveOwnedEntity | 33.47 ms | 2.517 ms | 6.630 ms | 31.38 ms | 1.03 | 0.26 | - | - | 19.59 MB | 1.00 |
| AddAndSaveComplexProperty | 7,969.44 ms | 138.437 ms | 122.721 ms | 7,943.61 ms | 245.40 | 38.14 | 159000.0000 | 64000.0000 | 7610.72 MB | 388.46 |
```
Project:
[EfCoreJsonBenchmark.zip](https://github.com/user-attachments/files/29979292/EfCoreJsonBenchmark.zip)
I couldn't find anything in the documentation about the tradeoffs of using complex types, so I am unsure whether this is expected behavior due to the structural equality that comes with `.ComplexProperty`?
### Your code
[EfCoreJsonBenchmark.zip](https://github.com/user-attachments/files/29979292/EfCoreJsonBenchmark.zip)
### Stack traces
```text
```
### Verbose output
```text
```
### EF Core version
10.0.9
### Database provider
Microsoft.EntityFrameworkCore.SqlServer
### Target framework
.NET 10
### Operating system
Windows 11
### IDE
JetBrains Rider 2026.1.4
Contributor guide
Research direction
Start with the attached EfCoreJsonBenchmark project and compare the DbSet.Add and DbContext.SaveChangesAsync paths for nested ComplexProperty mappings versus owned entities. Profile the allocation and timing differences on the reported EF Core 10.0.9 setup; done means identifying whether the regression is actionable or documenting the expected tradeoff.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100