You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I recently noticed an issue with seemingly non-related data getting lost on changes to owned entities.
We have an aggregate with several owned entities. Some of these owned entities own other nested entities, and in one case, the owned entity owns a list of nested entities. The code snippet below shows a minimal example of this configuration. When the owned entity is updated and the nested entity list is unchanged, the list is always empty after saving the changes to the database.
The owned entity is properly updated before saving the changes, the list content is only lost when SaveChanges is called. Non-List onwed entities (eg. x.OwnsOne(..., y => y.OwnsOne(...))) are not affected by this issue.
I'm happy to help, if there is any additional information you might need to analyse this issue. I didn't see any issues or documentation mentioning this, if this is known or expected/intended behaviour, please just let me know.
Version information
EF Core version: Tested with 8.0.10 and 8.0.5
Database provider: Tested with PostgreSQL and SQLite
Target framework: .NET 8.0
Operating system: Tested on Windows 11 and macOS 15, and in aspnet Docker image
classProgram{staticasync Task Main(string[]args){
Console.WriteLine("1: Testing without writing data to db");
TestWithoutDb();
Console.WriteLine("\n---\n");
Console.WriteLine("2: Testing with writing data to db");await TestWithDb();}privatestaticasync Task TestWithDb(){awaitusingvardb=new DemoContext();await db.Database.EnsureCreatedAsync();
Console.WriteLine("Creating a test contact.");varcontact=new Contact(){Id= Guid.NewGuid(),};varcompanyInfo=new CompanyInfo(){Name="CompanyName",Tags=[new Tag(){Name="Tag1"}]};
contact.AddCompanyInfo(companyInfo);await db.AddAsync(contact);await db.SaveChangesAsync();
Console.WriteLine("Created test contact. Data before update:");
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(contact));
Console.WriteLine("Updating the test contact.");
contact.ChangeCompanyName("ChangedCompanyName");await db.SaveChangesAsync();
Console.WriteLine("Updated the test contact. Data after update:");
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(contact));
Console.WriteLine("Deleting the test contact.");
db.Remove(contact);await db.SaveChangesAsync();}privatestaticvoidTestWithoutDb(){
Console.WriteLine("Creating a test contact.");varcontact=new Contact(){Id= Guid.NewGuid(),};varcompanyInfo=new CompanyInfo(){Name="CompanyName",Tags=[new Tag(){Name="Tag1"}]};
contact.AddCompanyInfo(companyInfo);
Console.WriteLine("Created test contact. Data before update:");
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(contact));
Console.WriteLine("Updating the test contact.");
contact.ChangeCompanyName("ChangedCompanyName");
Console.WriteLine("Updated the test contact. Data after update:");
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(contact));}}
Sample output
Tags content is dropped when writing to database.
1: Testing without writing data to db
Creating a test contact.
Created test contact. Data before update:
{"Id":"1b6a4e9b-90c9-49c4-a575-480ce4557edc","CompanyInfo":{"Name":"CompanyName","Tags":[{"Name":"Tag1"}]}}
Updating the test contact.
Updated the test contact. Data after update:
{"Id":"1b6a4e9b-90c9-49c4-a575-480ce4557edc","CompanyInfo":{"Name":"ChangedCompanyName","Tags":[{"Name":"Tag1"}]}}
---
2: Testing with writing data to db
Creating a test contact.
Created test contact. Data before update:
{"Id":"5136e52f-f554-4485-a08a-d8a695333dc1","CompanyInfo":{"Name":"CompanyName","Tags":[{"Name":"Tag1"}]}}
Updating the test contact.
Updated the test contact. Data after update:
{"Id":"5136e52f-f554-4485-a08a-d8a695333dc1","CompanyInfo":{"Name":"ChangedCompanyName","Tags":[]}}
Deleting the test contact.
Process finished with exit code 0.
The text was updated successfully, but these errors were encountered:
I recently noticed an issue with seemingly non-related data getting lost on changes to owned entities.
We have an aggregate with several owned entities. Some of these owned entities own other nested entities, and in one case, the owned entity owns a list of nested entities. The code snippet below shows a minimal example of this configuration. When the owned entity is updated and the nested entity list is unchanged, the list is always empty after saving the changes to the database.
The owned entity is properly updated before saving the changes, the list content is only lost when
SaveChanges
is called. Non-List onwed entities (eg.x.OwnsOne(..., y => y.OwnsOne(...))
) are not affected by this issue.I'm happy to help, if there is any additional information you might need to analyse this issue. I didn't see any issues or documentation mentioning this, if this is known or expected/intended behaviour, please just let me know.
Version information
EF Core version: Tested with 8.0.10 and 8.0.5
Database provider: Tested with PostgreSQL and SQLite
Target framework: .NET 8.0
Operating system: Tested on Windows 11 and macOS 15, and in aspnet Docker image
Code
Config - Minimal reproducible example
Demo Console App
Sample output
Tags
content is dropped when writing to database.The text was updated successfully, but these errors were encountered: