dotnet / dotnet/efcore

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

Open
#31,540 5 comments 1 reaction 1 assignee Claimed by @AndriySvyryd View on GitHub
area-change-tracking area-owned-entities customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

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 operation, System.Func> 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
{
public void Configure(EntityTypeBuilder 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

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.