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)?