Skip to content
Permalink
Browse files

Only use reference equality in EntityCollection

See #118 for details
  • Loading branch information
Turnerj committed Dec 3, 2019
1 parent d57759e commit bb711c28b757f265abce9ae43526f6c373429227
@@ -28,7 +28,7 @@ public EntityEntry<TEntity> GetEntry(TEntity entity)

foreach (var entry in Entries)
{
if (Equals(entityId, defaultIdValue) && entry.Entity.Equals(entity))
if (Equals(entityId, defaultIdValue) && ReferenceEquals(entry.Entity, entity))
{
return entry;
}
@@ -55,7 +55,7 @@ public void Update(TEntity entity, EntityEntryState state)
var entry = GetEntry(entity);
if (entry != null)
{
if (entry.Entity.Equals(entity))
if (ReferenceEquals(entry.Entity, entity))
{
entry.State = state;
}
@@ -14,6 +14,28 @@ public class EntityCollectionModel
public string Title { get; set; }
}

public class EntityCollectionOverriddenEqualsModel
{
public string Id { get; set; }

public string EqualsProperty { get; set; }

public override bool Equals(object obj)
{
if (obj is EntityCollectionOverriddenEqualsModel model)
{
return EqualsProperty == model.EqualsProperty;
}

return false;
}

public override int GetHashCode()
{
return EqualsProperty?.GetHashCode() ?? base.GetHashCode();
}
}

[TestMethod]
public void AddNewEntry()
{
@@ -65,6 +87,24 @@ public void UpdateExistingEntryInstanceMatch()
Assert.IsTrue(entityCollection.GetEntries().All(e => e.Entity == entity && e.State == EntityEntryState.NoChanges));
}

[TestMethod]
public void EntryDoesntMatchOnEqualityOverride()
{
var entityCollection = new EntityCollection<EntityCollectionOverriddenEqualsModel>();
var entityA = new EntityCollectionOverriddenEqualsModel
{
EqualsProperty = "DbEntityCollectionTests.EntityCollectionOverriddenEqualsModel"
};
entityCollection.Update(entityA, EntityEntryState.Added);

var entityB = new EntityCollectionOverriddenEqualsModel
{
EqualsProperty = "DbEntityCollectionTests.EntityCollectionOverriddenEqualsModel"
};
entityCollection.Update(entityB, EntityEntryState.Added);
Assert.AreEqual(2, entityCollection.GetEntries().Count());
}

[TestMethod]
public void RemoveRange()
{

0 comments on commit bb711c2

Please sign in to comment.
You can’t perform that action at this time.