For some reason, NHibernate is telling me it can't convert NHibernate.Collection.Generic.PersistentGenericSet[Ingredient]
to System.Collection.Generic.IList[Ingredient]
, when I try to get the data from the database. This is a simplfied version of my class mapping/implementation:
public class Product {
protected Product() { };
public virtual Name { get; set; }
public virtual IList<Ingredient> {
get { return new List<Ingredient>(ingredients).AsReadOnly(); }
protected set { ingredients = value; }
}
private IList<Ingredient> ingredients = new List<Ingredient>();
}
--
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="LanchesAdmin.Core.Domain.Product, LanchesAdmin.Core" table="product" >
<id name="ID" generator="identity">
<column name="id" not-null="true" />
</id>
<property name="Name" column="name" length="64" not-null="true" />
<set name="Ingredients" table="ingredient" fetch="join" lazy="true" inverse="true">
<key column="product_id" />
<one-to-many class="LanchesAdmin.Core.Domain.Ingredient, LanchesAdmin.Core" />
</set>
</class>
</hibernate-mapping>
I've seen several related questions, but they all tell me to use IList, and that's exactly what I'm doing.
The reason is that you are using a set. And PersistentGenericSet<T>
simply doesn't implement the IList<T>
interface.
I think you should simply make the property of type IEnumerable<Ingredient>
.
This solves two problems:
Either change type of the property to ISet
from IList
.
Or change mapping from <set>
to <list>
.
Do you want a list, bag or set? Your domain model contains a IList<T>
but you've mapped it as a <set>
.
Either change your mapping to a <bag>
or <list>
or change your domain model to use a ISet<T>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With