Open davidferneding opened 3 days ago
Additional info: A workaround for this is to explicitly create a new list and copying the content:
public void ChangeCompanyName(string companyName)
{
if (CompanyInfo is null) return;
CompanyInfo = CompanyInfo with
{
Name = companyName,
Tags = CompanyInfo.Tags.Select(x => new Tag() { Name = x.Name }).ToList()
};
}
Reusing the old list does not work, the object reference needs to change:
public void ChangeCompanyName(string companyName)
{
if (CompanyInfo is null) return;
CompanyInfo = CompanyInfo with
{
Name = companyName,
Tags = CompanyInfo.Tags // doesn't work
};
}
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.