-
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
Some added entities are removed from SortedSet after SaveChanges() #16161
Comments
@ajcvickers does this ring any bells? |
@ajcvickers Thanks for the hint! 😁 If I understood your comment in #15687 correctly, this should be the correct way to override public class Level1
{
public int Id { get; set; }
[Required]
public ICollection<Level2> Children { get; set; } = new SortedSet<Level2>();
}
public class Level2 : IComparable<Level2>
{
public int Id { get; set; }
public int Radius { get; set; }
public string Name { get; set; }
public Level1 Level1 { get; set; }
public int Level1Id { get; set; }
public int CompareTo(Level2 other)
{
return StringComparer.InvariantCultureIgnoreCase.Compare(Name, other.Name);
}
public override bool Equals(object obj)
{
if (obj is null || GetType() != obj.GetType())
{
return false;
}
var other = (Level2)obj;
return ReferenceEquals(this, obj)
|| Radius == other.Radius
&& Name.Equals(other.Name, StringComparison.InvariantCultureIgnoreCase);
}
public override int GetHashCode()
{
return HashCode.Combine(Radius, Name);
}
} This however still results in the same issue for me... |
@iokill Have you verified that EF is not given multiple instances that both represent the same entity? That is, if two instances compare equal but are both used with the same context instance, then things will break. EF can only track a single instance for a given entity. |
@ajcvickers Hmm... that is definitely an issues with the second However, I now completely removed the custom Also, is there some documentation on the requirements EF Core has on Thanks! |
@iokill Thanks for being patient. I was able to reproduce this on 2.2.4, so it looks like a potential bug.
Yes, this is correct. We should document it better. /cc @divega |
I have created dotnet/EntityFramework.Docs#1541. Feel free to add more details to it. |
Hi!
I ran into another issue with EF Core 2.1 and
SortedSet
(I did not check if this also occurs on 2.2 since I'm stuck on 2.1 for now).
Let's consider the following model where
Level1.Children
is aSortedSet
of
Level2
entries. The set sorts its entries byLevel2.Name
.My code loads an existing
Level1
entity (with related data), doeslvl1.Children.Clear()
and (re-)adds some
Level2
entries. After callingSaveChanges()
,lvl1.Children
is missing some entries. More specifically, it looses those entries there existed before, but were removed before adding new
Level2
entries.Here's the corresponding code:
There are a few things that I found out during testing:
If I remove the property
Level2.Level1Id
, the problem goes away and the collectioncontains the expected amount of entries after
SaveChanges()
.If I modify
Level2.CompareTo(other)
such that it only ever returns 0 whenthis.Equals(other)
, the issues also goes away.To me, this looks like an issue with change tracking in EF Core, since simply removing
the property
Level2.Level1Id
gets rid of the issue. But since I did not do a deep dive into the EFCore internals, I'm just guessing 😉Steps to reproduce
I've pushed a test case here: https://github.com/iokill/EFCoreCollectionSaveBug/blob/master/EFCoreSortedSetTestCase/Program.cs
Further technical details
EF Core version: 2.1.11
Database Provider: Microsoft.EntityFrameworkCore.Sqlite
Operating system: macOS 10.14.5
The text was updated successfully, but these errors were encountered: