Repository navigation
Description
While working on a project with .NET 5.0 and EFCore 5.0.3, I found the following issue.
I am using an entity that contains twice the same owned type with different property. If I create an entity with two different instances of the owned type, it works. If I create an entity with the same instance of the owned type used twice, it breaks.
I found the issue #24541 that has the same exception but I am not sure if it is the same issue.
I was also able to reproduce the problem with the code below in a unit test off the main branch of efcore:
private class OwnedContext : DbContext
{
private readonly string _databaseName;
public OwnedContext(string databaseName)
{
_databaseName = databaseName;
}
protected internal override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseInMemoryDatabase(_databaseName);
protected internal override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Owner>().HasKey(x => x.Id);
builder.Entity<Owner>().Property(x => x.Id).ValueGeneratedOnAdd();
builder.Entity<Owner>().OwnsOne(x => x.First);
builder.Entity<Owner>().OwnsOne(x => x.Last);
}
}
private class Owner
{
public long Id { get; set; }
public Owned First { get; set; }
public Owned Last { get; set; }
}
private record Owned(long A, long B, long C, long D);
[ConditionalFact]
public void OwnedEntity_uses_same_instance_twice()
{
using var context = new OwnedContext(Guid.NewGuid().ToString());
var owned = new Owned(1,2,3,4);
var parent = new Owner()
{
First = owned,
Last = owned
};
context.Add(parent);
context.SaveChanges();
}A workaround for the problem above is to clone the second instance e.g.:
Last = owned with { }The above test produces the following exception:
System.InvalidOperationException
The value of 'Owner.Last#Owned.OwnerId' is unknown when attempting to save changes. This is because the property is also part of a foreign key for which the principal entity in the relationship is not known.
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.PrepareToSave() in D:\Dev\Dotnet\efcore\src\EFCore\ChangeTracking\Internal\InternalEntityEntry.cs:line 1338
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.GetEntriesToSave(Boolean cascadeChanges) in D:\Dev\Dotnet\efcore\src\EFCore\ChangeTracking\Internal\StateManager.cs:line 955
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(StateManager stateManager, Boolean acceptAllChangesOnSuccess) in D:\Dev\Dotnet\efcore\src\EFCore\ChangeTracking\Internal\StateManager.cs:line 1168
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<>c.<SaveChanges>b__104_0(DbContext _, ValueTuple`2 t) in D:\Dev\Dotnet\efcore\src\EFCore\ChangeTracking\Internal\StateManager.cs:line 1157
at Microsoft.EntityFrameworkCore.Storage.NonRetryingExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded) in D:\Dev\Dotnet\efcore\src\EFCore\Storage\NonRetryingExecutionStrategy.cs:line 48
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess) in D:\Dev\Dotnet\efcore\src\EFCore\ChangeTracking\Internal\StateManager.cs:line 1154
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess) in D:\Dev\Dotnet\efcore\src\EFCore\DbContext.cs:line 519
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges() in D:\Dev\Dotnet\efcore\src\EFCore\DbContext.cs:line 480
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.OwnedFixupTest.OwnedEntity_uses_same_instance_twice() in D:\Dev\Dotnet\efcore\test\EFCore.Tests\ChangeTracking\Internal\OwnedFixupTest.cs:line 73
EF Core version: 5.0.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.10
Activity
ajcvickers commentedon Apr 9, 2021
Related #20474
ajcvickers commentedon Apr 16, 2021
Note for triage: validated that the warning message is generated from the repro code given:
chrisdreams13 commentedon May 20, 2022
Is there some fix related to this bug? I had the same problem but a little bit tricky.
I divided my solution into several projects, the DbContext is separated by the owned class (in my case, Position) and throws the same error, here is the debug trace with both, the warning and the exception:
The curious thing is if I create an entity class inside the db context project that extends from my original class works ok
with this trace
I could use this last for my purpose, but it's unnecessary an extra wrapping class
My ModelBuilder (all code is the same in both tests)
Operating system: Windows 10
IDE: Visual Studio 2022 17.1.5
SDK Version: 6.0.203
DbContextProject
Microsoft.EntityFrameworkCore.Relational:6.0.5
StatupProject:
Microsoft.EntityFrameworkCore:6.0.5
Microsoft.EntityFrameworkCore.Proxies:6.0.5
Microsoft.EntityFrameworkCore.SqlServer
DesktopProject
TargetFramework:net6.0-windows10.0.19041.0
TargetPlatformMinVersion:10.0.17763.0
1 remaining item
bbartels commentedon May 4, 2025
Just ran into this, took a long time to realize what was going on. Would be great to see this fixed (or allow complex types on owned entities)
JoasE commentedon Nov 27, 2025
I would suggest at-least adding a specific exception for this to help users who run into this determine the issue
[-]Multiple owned entities fail to save when they are set to the same instance[/-][+]Throw by default for multiple owned entities set to the same instance[/+]Throw by default when same instance is used for multiple owned entity…
AndriySvyryd commentedon Jul 9, 2026
Fixed in 2c99b9a