NHibernate Custom Collections hack在事务外部工作,但不在内部工作

NHibernate Custom Collections hack在事务外部工作,但不在内部工作,nhibernate,collections,Nhibernate,Collections,按照所描述的技术,我能够填充一个域对象,该对象为其子对象使用自定义集合。相关属性映射如下所示: <component name="Contacts" class="My.CustomList`1[[Domain.Object, DomainAssembly]], MyAssembly"> <set name="InnerList"> <key column="PARENT_ID" /> &

按照所描述的技术,我能够填充一个域对象,该对象为其子对象使用自定义集合。相关属性映射如下所示:

    <component name="Contacts" class="My.CustomList`1[[Domain.Object, DomainAssembly]], MyAssembly">
        <set name="InnerList">
            <key column="PARENT_ID" />
            <one-to-many class="Contact" />
        </set>
    </component>
这就像从数据库加载数据的魅力一样,不必放弃我非常有用的自定义集合类

然后,我继续尝试实现保存,并遵循建议,决定将对NHibernate的每个调用都封装在一个事务中

现在,当我在加载后提交时,NHibernate抛出一个
InvalidCastException
:“无法将类型为“my.CustomList`1[Domain.object,DomainAssembly]”的对象强制转换为类型为“Iesi.Collections.ISet”。”

有没有办法让这一切按我所期望的方式进行

编辑:


遵循Raphael提供的指导,我尝试切换到
ICollection
,这在我提交事务时为我提供了一个不同的
InvalidCastException
:无法将类型为“My.CustomList`1[Domain.object]”的对象强制转换为类型为“NHibernate.Collection.IPersistentCollection”。

将属性更改为类型

IList<T>
IList

InnerList(因此也就是您的CustomCollection)在创建时是否实现了Iesi.Collections中的ISet?如果没有-这就是你的问题所在。我试图避免在我的域中依赖Iesi.Collections,这就是我首先使用“hack”的原因。使用IList和集合映射会导致初始查询在尝试强制转换NHibernate.Collection.Generic.PersistentGenericSet
1[MyDomain.Contact]时失败到System.Collections.Generic.IList
1[MyDomain.Contact]。PersistentGenericSet支持ICollection和ICollection,但不支持IList。
IList<T>