-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Storing Same Owned Entity In Json Column Does Not Work #33967
Comments
When the code move the same instance like this, it is changing the relationship of that single instance. So it is first associated with passenger 1, then removed from passenger 1 and associated with passenger 2, and so on. A single object instance cannot be used to represent multiple different entities. This is one of the reasons why complex types are a better fit than entity types for mapping to JSON. |
@ajcvickers I see. I think that the confusing part is that Reading your EF Core 8 RC1: Complex types as value objects makes it clear to me that we should switch the modelling of There's also #32799 which to a large extent seems to be a duplicate of this issue here. So, if you don't plan on any changes - even throwing an error, which seems to be hard to implement according to your comment here - we can close this issue. Thanks a lot for your valuable input. |
Unfortunately, this is not how owned types work. They are still entity types and follow entity semantics.
Correct.
Yes, this is not a great place for us to be in, but prioritization of resources has been very difficult. We're working hard to get to a consistent space. |
Description
When the same instance of an object is referenced from multiple entities, it is only stored once and silently discarded in other parents when storing it in a JSON column. IMHO, at least an error should be thrown instead of just dropping the data if for some reason it is not possible to store a JSON serialized copy of "shared" instances or if that would lead to an undesirable design.
How to reproduce - the code
In order for you to reproduce easily, I've extracted the problem to this repository:
https://github.com/dradovic/MissingLocationJsonRepro
The repo contains 3 succeedingly stripped down cases in 3 branches.
main
branch: The story starts with an entity calledBoat
, wherePassengers
are stored in a JSON column.A
PassengerInfo
is reflecting some Person data:Populating some boats with passengers whose
Location
points to the same instance results in:As we can see, the location is only stored for the last boat's passenger but silently dropped for the others(!).
OnlyLocation
branch: I then went on to remove the intermediaryPassangerInfo
class and to try to store the locations directly.Again, data is silently discarded if we try to store the same location in multiple rows:
This time, JSON serialization does not say
null
but just discards the whole object.OnlyOneLocation
branch: in this branch, I've changed the collection to be a one-to-one relationship toLocation
.And this time, the data cannot even be persisted as I get a:
SqlException: Cannot insert the value NULL into column 'Passenger', table 'MissingLocationJsonRepro.dbo.Boats'
if I try to persist the same location referenced from multiple boats which shows that EF again tried to drop data.Include provider and version information
EF Core version:
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: Windows
IDE: Visual Studio 2022 17.10.1
The text was updated successfully, but these errors were encountered: