Skip to content

JSON mapping via complex types is significantly slower than via owned entities for nested hierachies #38616

Description

@Kaskadee

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<T>.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

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

Stack traces


Verbose output


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

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions