My data model looks like this.
public abstract class BasePivotField
{
public virtual int SystemId { get; set; }
public virtual int Version { get; set; }
public virtual string FieldName { get; set; }
public virtual string FieldValuePrefix { get; set; }
public virtual Field Field { get; set; }
public abstract PivotFieldType FieldType { get; }
}
public class PivotColumnField : BasePivotField
{
public virtual CriteriaWithField FieldCriteria { get; set; }
public virtual int Width { get; set; }
public override PivotFieldType FieldType
{
get { return PivotFieldType.ColumnField; }
}
}
public class PivotTableControl : BaseControl
{
public virtual Criteria Criteria { get; set; }
public virtual int Height { get; set; }
public virtual PagingProperties PagingProperties { get; set; }
public virtual IList<BasePivotField> PivotTableFields { get; set; }
}
I use Fluent NHibernate mapping and the mapping classes are below
public class PivotTableControlMap : SubclassMap<PivotTableControl>
{
public PivotTableControlMap()
{
References(x => x.Criteria).Cascade.All().Column("PivotTableCriteria");
Map(x => x.Height);
Component(x => x.PagingProperties);
HasMany(x => x.PivotTableFields)
.KeyColumn("PivotTableId")
.Cascade.AllDeleteOrphan()
.AsBag();
}
}
public class PivotTableFieldMap : ClassMap<BasePivotField>
{
public PivotTableFieldMap()
{
Table("PivotTableField");
Id(x => x.SystemId);
Version(x => x.Version);
Map(x => x.FieldName);
References(x => x.Field).Cascade.All().Column("FieldId");
Map(x => x.FieldValuePrefix);
DiscriminateSubClassesOnColumn("PivotFieldType");
}
}
public class PivotColumnFieldMap : SubclassMap<PivotColumnField>
{
public PivotColumnFieldMap()
{
Component(x => x.FieldCriteria).ColumnPrefix("PivotTable");
Map(x => x.Width);
}
}
There are other classes that inherit from BasePivotField, but I have left them out for brevity.
The problem is that when I load the PivotTableControls from the database using NHibernate, the PivotTableFields collection does not get loaded. The exception thrown says 'Creating a proxy instance failed'. Looking into the exception deeper, I can see that the PivotTableFields says 'illegal access to loading collection'. I can confirm that the Session is not closed. If I don't use lazy load, the problem still persists.
I have used a similar kind of model and mapping in other places and they work fine. What am I doing wrong here? Any clues will be highly appreciated!