Entity equality does not work in all cases


Hi Tim,
I have come across a very subtle bug in the EntityBase == operator overload method in the following statement:
if (base1.Key != base2.Key)
This does not work if the base1.Key and base2.Key properties don't reference the same Guid object, but have the same value. The statement does work if both Key properties reference the same Guid object. The fix for this bug is to replace the statement with this one:
if (base1.Key.Equals(base2.Key))
This second statement works fine in all cases. The reason for this has to do with the fact that the == and != operator overloads are not virtual whereas the Equals method is virtual. So, what happens in the first statement is that the != operator overload for the System.Object class is called and not the overload defined in the Guid class, since the Key property is declared as type object. This causes a reference comparison and not a value comparison. A value comparison is what is needed for this operator overload to work as needed. The second statement will cause the Equals method of the Guid struct to be called and not the one defined for the System.Object class, since it is virtual, and the correct value comparison between the two Guids is done, instead of a reference comparison.
Here is a unit test that demonstrates the issue:
    public void EntitiesAreEqualIfDifferentEntityAndKeyObjectsButSameKeyValue()
        EntityBase entity1 = new EntityBase();
        Guid id2 = new Guid(entity1.Key.ToString());
        EntityBase entity2 = new EntityBase(id2);
        Assert.AreEqual(entity1.Key, entity2.Key);
        Assert.AreEqual(entity1, entity2);
        Assert.IsTrue(entity1 == entity2);
        Assert.IsFalse(entity1 != entity2);
This unit test will fail on the second Assert. When I change the EntityBase == operator overload method with the new statement above, this unit test passes.
Closed May 12, 2011 at 5:35 AM by tmccart1


wrote May 12, 2011 at 5:35 AM

Resolved with changeset 60701.

tmccart1 wrote May 12, 2011 at 5:36 AM

Thanks for finding that rkanagy!

wrote Feb 21, 2013 at 10:42 PM

wrote May 16, 2013 at 10:20 AM