-
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
Issue with composite owned type primary key and non-reference equality #15584
Comments
Note for triage: my initial impression here is that the non-reference equality here is incompatible with EF Core tracking; removing it resolves the error. However, putting it here for others to look at in triage before closing. |
You in #14675
What?!
Closing?! =D |
@voroninp I was terse because I was just making a note for triage, not a resolution of the bug. To be a bit clearer, EF uses reference equality for everything it does when it can. However, in your code you are explicitly creating a navigation property that does not use reference equality: public HashSet<Owned> Owned { get; } = new HashSet<Owned>(); We know this has issues, and we're considering #14675 to make this better for lists, but this would not be something we can do for However, as I said, I would like others on the team to take a look first, hence my note for triage. |
@ajcvickers Hm... But why is type of navigation property important here? |
@voroninp Yes, |
Triage concurs with analysis. The |
@ajcvickers Ok, but this behaviour is not obvious at all. Can EF detect this situation and at least emit a warning or even throw an exception by default? If |
@voroninp I've investigated detecting this in the past but was not able to detect this situation in a robust and performant way. |
@ajcvickers Will value equality work for |
@voroninp Yes, but #14675 is a potential improvement in this for lists. But as a general rule you should avoid changing from reference equality for entity types, since EF can only track a single instance of a given entity identity. Doing otherwise results in lots of complications around merging graphs with potential conflicts. It's also worth noting that while owned types are sometimes considered "value objects", my personal view is that this is an anti-pattern. Value objects don't have keys and should have value semantics. This isn't a great fit for any type of entity type since it will never have value semantics and must have a key to be tracked, even if it is owned. |
@ajcvickers How do you offer to implement a collection of value objects? In my case key of principal entity is a shadow property, it's not even the part of value object, and all other properties just comprise the value itself. Composite key is just an implementation detail here. |
@ajcvickers I am still pondering over the problem. |
Hm, comparing the two hashsets correctly would require iterating over the left one and confirming that each entry is present in the right, and then also doing the reverse. (If you imagine a hashset that uses I suppose the performance would be on the same order as that of a custom implementation for lists. @ajcvickers Should you be interested, I have correct implementations for all common collection variants (luckily, not that many) lying around from an earlier project related to structural equality. @voroninp As for workarounds... Do you truly need a hashset, or is it just the most correct type to use? If there will not be many child entities, most solutions would work: replacing the set instead of appending to it, or using a list instead. There is If you truly need fast lookup with many elements, you could consider implementing your own
|
Originally posted by @voroninp here: #14675
The text was updated successfully, but these errors were encountered: