-
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
Duplicated entries in navigation collection if Equals and GetHashCode are overridden #15687
Comments
@leszekrogowski There are a couple of issues here. First, EF Core uses reference equality for entities, and requires that only one entity instance is tracked for any given entity identity. This means that entities that override Second, for many things (including common .NET collections like |
@ajcvickers Thanks for that explanation. I don't quite understand WHY they are duplicated in the HashSet, in memory, but I understand how to avoid it now. |
@ajcvickers Can we get some documentation and sample code on how to correctly implement equality operators, hashcodes when overriding in EF Core? Using the default code generation in Visual Studio only seems to make the matter worse. |
@James-Jackson-South This is a good start: https://ericlippert.com/2011/02/28/guidelines-and-rules-for-gethashcode/ |
If you override Equals and GetHashCode in entity it'll cause ChangeTracker strange behaviour for navigation collection. Child entities get duplicated when you're adding new entities to the collection.
Steps to reproduce
Please consider the following code:
Further technical details
EF Core version: 2.2.4
Database Provider: Checked on Microsoft.EntityFrameworkCore.InMemory and Microsoft.EntityFrameworkCore.SqlServer
Operating system:
IDE: Visual Studio 2017 15.9.11
The text was updated successfully, but these errors were encountered: