This repository demonstrates a bug in Entity Framework Core 10.0.2 where using C# record's with expression on an owned entity stored as JSON causes in-memory corruption after calling SaveChanges().
Link to EF core issue tracker: dotnet/efcore#37581
When working with owned entities stored as JSON and using C# record's with expression, EF Core corrupts the in-memory representation of nested owned entities after the second call to SaveChanges(). The data is correctly persisted to the database, but the in-memory object becomes corrupted.
- NullReferenceException when accessing nested properties of the owned entity
- Nested owned entities (e.g.,
Weight) becomenullin memory afterSaveChanges() - The corruption only affects the in-memory object graph
- Database contains the correct, uncorrupted data
- Re-querying the entity from the database returns the correct data
- Create an entity with an owned entity stored as JSON
- Add the entity to the context and call
SaveChanges()(first save) - Use the
withexpression to create a modified copy of the owned entity - Call
SaveChanges()again (second save) - Bug manifests: Nested properties of the owned entity become
nullin memory
- .NET Version: 10.0
- EF Core Version: 10.0.2
- Database: SQL Server 2022 (reproduced using Testcontainers)
- Language Features: C# records with
withexpression