C# - NHibernate 无法将 NHibernate.Collection.Generic.PersistentGenericSet 转换为 System.Collections.Generic.IList

标签 c# mysql nhibernate

出于某种原因,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,而这正是我正在做的。

最佳答案

原因是你用的是set。和 PersistentGenericSet<T>根本没有实现 IList<T>界面。

我认为您应该简单地创建 IEnumerable<Ingredient> 类型的属性.
这解决了两个问题:

  1. 你摆脱了你提到的错误
  2. 您的类的 API 直接表明您类的消费者不允许通过该属性添加成分。

关于C# - NHibernate 无法将 NHibernate.Collection.Generic.PersistentGenericSet 转换为 System.Collections.Generic.IList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12727025/

相关文章:

mysql - 从每组涉及两个字段的方程式中选择最高值

c# - 如何检查代理的空值

c# - 如何根据 C# 中的条件组合两个具有不同属性的列表

c# 从 Outlook 收件箱转发电子邮件

sql - 多个外键?

MySQL : issue with GROUP BY and COUNT clauses

c# - 匿名类型列表或对象列表

c# - 更新 wpf 中的 UI 元素

c# - NHibernate - 问号而不是查询中的值

nhibernate - id不在列表中的查询