Problem prefetching one-to-many when overriding Equals() and GetHashCode()

Posts   
 
    
lomvgen
User
Posts: 5
Joined: 11-Oct-2012
# Posted on: 11-Oct-2012 08:54:21   

I'm using LLBLGen 2.6 Final with runtime version 2.6.9.511

I have two tables on a SQL-Server 2005: Alarm and AlarmState

Alarm has columns Id (int, PK), AlarmTime, Family, Text and several others. AlarmState has columns Id (int, PK), AlarmId (int, FK to Alarm), AlarmState, Annotation

Alarm is joined on column Id one-to-many to AlarmState on AlarmId. Multiple states per alarm are possible.

When I call


IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.AlarmEntity);
prefetchPath.Add(AlarmEntity.PrefetchPathAlarmState);
AlarmEntity alarm = new AlarmEntity(4711);
DataAccessAdapter adapter = new DataAccessAdapter();
adapter.FetchEntity(alarm, prefetchPath);

everything works as expected and the alarms states are prefetched and filled into to AlarmState collection of the alarm. Same works when I ask for collections of alarms.

Now I wanted to use AlarmEntity as a key in a Dictionary<AlarmEntity, ....> In this context alarms are equal if they have the same AlarmTime and Family.

So I have overridden the Equals() and GetHashCode() methods of AlarmEntity in a partial class.

Pseudo code:


public override bool Equals(object obj)
{
    if (obj == null)
    {
        return false;
    }

    var ae = obj as AlarmEntity;
    if (ae == null)
    {
        return false;
    }

    return this.AlarmTime == ae.AlarmTime && this.Family == ae.Family;
}

public override int GetHashCode()
{
    int hashCode = 17;
    hashCode = 37 * hashCode + AlarmTime.GetHashCode();
    hashCode = 37 * hashCode + Family.GetHashCode();
    return hashCode;
}

Now I can use the AlarmEntity as expected in the Dictionary. But the prefetch of the alarm states no longer works on calls on single alarms (and alarm collections).

When activating LLBLGen tracing I see that the correct sql queries are called. It seems that the states cannot be assigned to the alarm state collection of the correct alarm entities.

In debugging I see that GetHashCode() is called on fetching the alarm entity but not the Equals method.

Ideas how get both prefetching and usage of Equals (with GetHashCode)?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 11-Oct-2012 10:48:04   

Please first download the latest v2.6 build from the customer area, then try again. You're using a runtime that's 3 years old.

Frans Bouma | Lead developer LLBLGen Pro
lomvgen
User
Posts: 5
Joined: 11-Oct-2012
# Posted on: 15-Oct-2012 10:27:20   

Otis wrote:

Please first download the latest v2.6 build from the customer area, then try again. You're using a runtime that's 3 years old.

Thanks, that really helped, with 2.6.12.829 everything works as expected!