【发布时间】:2012-09-25 11:23:55
【问题描述】:
由于某种原因,当我尝试从数据库中获取数据时,NHibernate 告诉我它无法将 NHibernate.Collection.Generic.PersistentGenericSet[Ingredient] 转换为 System.Collection.Generic.IList[Ingredient]。这是我的类映射/实现的简化版本:
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>
我看过几个相关的问题,但他们都告诉我使用 IList,而这正是我正在做的。
【问题讨论】:
-
尝试删除新列表的创建和 ReadOnly 调用。
-
@Euphoric,试过了,没啥区别。
-
@Raphael_ 你能发布你最终工作的代码吗?如果可能的话,您能否发布
Ingredient的代码(或解释它是什么)。我遇到了一个问题,想知道您的解决方案是否能解决它。提前致谢。 -
@REMESQ 这是整个项目:dropbox.com/s/kgfha79lr6yz6cy/LanchesAdmin.rar 映射和实现应该在
LanchesAdmin.Core/Domain上。 PS:这是相当业余的代码,但我希望它能给你一些启发。 -
感谢您的启发!
标签: c# mysql nhibernate