Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#31540·efcore

Improve exception message for tracking multiple nested owned entities sharing same instance

Author: royvandertuukCreated Aug 23, 2023Updated Sep 28, 2026
Labelscustomer-reportedarea-change-trackingarea-owned-entities

My model nests 2 levels of owned entities and has multiple properties of the same type contained in a single entity.

Structure:

  • Foo
    • Statistics FooStatistics (Owned)
      • PercentileRange Percentile25En75 (Owned)
        • double Low
        • double High
      • PercentileRange Percentile10En90 (Owned)
        • double Low
        • double High
      • double Median
    • Statistics BarStatistics (Owned)
      • PercentileRange Percentile25En75 (Owned)
        • double Low
        • double High
      • PercentileRange Percentile10En90 (Owned)
        • double Low
        • double High
      • double Median

When I create an instance of Foo and add this to the DBContext and call the SaveChanged method the following error is thrown.

Error

System.InvalidOperationException: 'Cannot save instance of 'Foo.BarStatistics#Statistics.Percentile25En75#PercentileRange' because it is an owned entity without any reference to its owner. Owned entities can only be saved as part of an aggregate also including the owner entity.

I am saving the aggregate root (Foo) therefore I do not understand why the error is thown. If I were to save the Statistics without the Foo I would understand that such an error would be thrown. Perhaps I made a mistake in the configuration, but I can't detect any.

Stacktrace

Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.PrepareToSave() Line 1572	C#
 	Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.GetEntriesToSave(bool cascadeChanges) Line 1047	C#
 	Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager stateManager, bool acceptAllChangesOnSuccess) Line 1256	C#
 	Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges.AnonymousMethod__107_0(Microsoft.EntityFrameworkCore.DbContext _, (Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager StateManager, bool AcceptAllChangesOnSuccess) t) Line 1246	C#
 	Microsoft.EntityFrameworkCore.SqlServer.dll!Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute<(Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager, bool), int>((Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager, bool) state, System.Func<Microsoft.EntityFrameworkCore.DbContext, (Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager, bool), int> operation, System.Func<Microsoft.EntityFrameworkCore.DbContext, (Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager, bool), Microsoft.EntityFrameworkCore.Storage.ExecutionResult<int>> verifySucceeded) Line 57	C#
 	Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(bool acceptAllChangesOnSuccess) Line 1242	C#
 	Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.DbContext.SaveChanges(bool acceptAllChangesOnSuccess) Line 631	C#
 	Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.DbContext.SaveChanges() Line 582	C#
 	EFCoreOwnedEntitiesError.dll!EFCoreOwnedEntitiesError.Program.TestFoo() Line 39	C#
 	EFCoreOwnedEntitiesError.dll!EFCoreOwnedEntitiesError.Program.Main(string[] args) Line 9	C#

Entity configuration

internal sealed class FooEntityTypeConfiguration : IEntityTypeConfiguration<Foo>
{
    public void Configure(EntityTypeBuilder<Foo> builder)
    {
        builder.OwnsOne(foo => foo.FooStatistics, statisticsBuilder =>
        {
            statisticsBuilder.OwnsOne(statistics => statistics.Percentile25En75);
            statisticsBuilder.OwnsOne(statistics => statistics.Percentile10En90);
        });

        builder.OwnsOne(foo => foo.BarStatistics, statisticsBuilder =>
        {
            statisticsBuilder.OwnsOne(statistics => statistics.Percentile25En75);
            statisticsBuilder.OwnsOne(statistics => statistics.Percentile10En90);
        });
    }
}

Code

The following sample repository can be pulled to reproduce the issue. https://github.com/royvandertuuk/EFCoreIssueOwnedEntities/tree/main

Version information

EF Core version: Microsoft.EntityFrameworkCore.SqlServer (7.0.10) Target framework: net7.0 Operating system: Microsoft Windows 11 Pro IDE: Visual Studio 2022 Version 17.7.1

Source: dotnet/efcore

View original on GitHubView discussion on GitHub