This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi all, could someone point out to me the best possibility to implement Equals and GetHashCode on designer generated entities? All ways which I have tried so far result in null-reference or duplicate-id exceptions!? Kind regards |
|
|
You don't normally need to override Equals and GetHashCode on entities because within a unit of work LightSpeed ensures reference identity anyway: that is, once Widget #2 is loaded, any association or query that needs to return Widget #2 will return that same instance. So the default reference equality and hash code behaviour works within a unit of work. And you should normally be operating within a single unit of work except when explicitly attaching or detaching entities, so this should suffice for most scenarios. In fact overriding the default equality semantics may have unforeseen side effects as internally there are a couple of places where we store entities in a dictionary, and having non-reference-equal entities being equal (or having the hash code change over time, as it would do if you used mutable properties as part of the hash code), could cause this to go awry. That said, if you do need to override Equals and GetHashCode, you should be able to do it in the normal way by using the identity and/or other immutable properties. Here's a crude example using identity equality semantics: partial class Widget { Again, however, I'd generally advise against this. If you need a specialised equality comparer for your own dictionary, I'd suggest creating a class which implements IEqualityComparer<T> and passing that into the dictionary constructor, rather than providing a global override of Equals and GetHashCode at the entity level. |
|