-
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
Same reference type instance shared by multiple entities is only saved to the last entity's JSON column #32799
Comments
@tanemann EF doesn't support sharing the same instance of an entity type in multiple places. Complex types will be more appropriate for this case once fully implemented. |
@ajcvickers thanks for the reply. I'm still not quite sure why EF is behaving the way that it is in the described scenario. The Blog Authors in this case are not meant to be relational, but stored individually for each Blog entity (as JSON). After saving the entities, there should no longer be any dependency between the Author columns in the DB, or the Writer instances that are created when reading/querying Blog entities later on. I admit the overly simplified example is a bit misleading, since in a real-life situation the Blog Author would obviously be a relation between the Blog entity and a Writer, which would in that case also be an entity. In any case, even if EF is not meant to support using a shared instance for a JSON column property, I don't think the current behaviour is at all rational. EF should throw an error, or at least somekind of a warning if attempting to use a shared instance, rather than just happily saving the property only for the last entity that the instance is assigned to. The current implementation leads to data loss without giving any kind of a sign that that is happening, which is something that should be avoided at all cost. |
This has been discussed many times in the past. Doing so would require that we do significant extra work (and this will need additional data structures for tracking) to detect a case which is programmer error, and so should never occur outside of bugs in the application code. We came down on the side of perf for correct code in this case. Also, keep in mind that, as I mentioned above, mapping entity types to JSON is, perhaps, the real issue here. Complex types are a much better semantic match. |
I think you're absolutely right, complex types do indeed seem align much better with our needs. Until #31252 is implemented, I think our options are to either keep using owned entities, while being more careful with property assignments or just use a json-serializing value converter, since we don't really need json-querying capabilities for the time being. Thanks for your help and valuable input! |
It seems that when using JSON columns, if you use a single shared instance of a reference type for multiple entities and then call SaveChanges, only the last row will actually include the serialized JSON in the SQL INSERT. This happens in EF7 and EF8, using either SQL Server or SQLite.
Simple data model:
Using JSON column for Author:
Adding blogs with identical Author using a shared instance of Writer:
Output to console when console logging enabled:
Note that only the last INSERT included the serialized Author JSON.
For comparison, the issue does not occur when each Blog uses their own Writer instance:
Output:
The text was updated successfully, but these errors were encountered: