I have a class with a custom list which inherits from List and cannot get the NHibernate mapping working.
public class MyClass
{
private MyList<Foo> foos;
public virtual MyList<Foo> Foos
{
get { return foos; }
set { foos= value; }
}
}
<bag name="Foos" access="property" cascade="all-delete-orphan" batch-size="5">
<key column="MyClassId"/>
<one-to-many class="Domain.Model.MyClass, Domain"/>
</bag>
I got the exception
Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag
1[Domain.Model.Foo]' to type 'Domain.Model.MyList
1[Domain.Model.Foo]'.
Following this blog, I tried to wrap the bag in a component,
<component name="Foos" access="nosetter.camelcase-underscore">
<bag name="Foos" access="property" cascade="all-delete-orphan" batch-size="5">
<key column="MyClassId"/>
<one-to-many class="Domain.Model.MyClass, Domain"/>
</bag>
</component>
resulting in the error
Could not find a getter for property 'Foos' in class 'Domain.Model.MyList`1[Domain.Model.Foo]'
MyList has only a method to add objects.
public class MyList<T> : List<T>
{
public new void Add(T item)
{
//custom stuff
base.Add(item);
}
}
This should work...
public class MyClass
{
public virtual IList<Foo> Foos { get; private set; }
public MyClass()
{
Foos = new MyList<Foo>();
}
}
Changed the mapped property to an IList
and set it to MyList