subsonic / SubSonic-3.0

SubSonic 3.0 for the .NET 3.5 Framework
http://subsonic.github.io/
558 stars 209 forks source link

ActiveRecord - Equals() method is broken #207

Open awbacker opened 14 years ago

awbacker commented 14 years ago

The equals method is broken when the underlying column is a GUID. Because the "==" operator is used, Guids do not compare as equal. The object.Equals() method, and the method in general needs to be re-written to be a bit safer. I'm not claiming this is an ideal implementation though:

    public override bool Equals(object obj){
        if (obj == null || (GetType() != obj.GetType()) || KeyValue() == null) return false;
        <#=tbl.ClassName#> compare=(<#=tbl.ClassName#>)obj;
        return KeyValue().Equals(compare.KeyValue());
    }
adam7 commented 14 years ago

I've come across this myself and fixed it in the templates I use. I'll fix.

gabrielrdz commented 11 years ago

It does not only affect GUIDs, it affects all types (i bumped into the bug with a class that had an int key value). I think the problem is the implicit cast that happens when the value is returned as object by the KeyValue() method.