获取"无法将PersistentGenericSet强制转换为ISet"错误

dan*_*iax 27 c# nhibernate automapping

我收到此错误:

无法转换'NHibernate.Collection.Generic.PersistentGenericSet 1[IocWinFormTestEntities.People]' to type 'System.Collections.Generic.ISet1 [IocWinFormTestEntities.People]' 类型的对象.

实体:

public class Event 
{
    public Event()
    {
        this.People = new HashSet<People>();
    }
    public virtual Guid Id { get; private set; }

    public virtual ISet<People> People { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

地图覆盖类:

public class EventMapOverride : IAutoMappingOverride<Event>
{
    public void Override(AutoMapping<Event> mapping)
    {
        mapping.HasMany(c => c.People)
            .AsSet()
            .Cascade.AllDeleteOrphan();
    }
}
Run Code Online (Sandbox Code Playgroud)

从流畅的自动播放器生成的hbm:

<set cascade="all-delete-orphan" name="People">
    <key>
        <column name="Event_id" />
    </key>
    <one-to-many class="IocWinFormTestEntities.People, IocWinFormTestEntities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</set>
Run Code Online (Sandbox Code Playgroud)

怎么了?

Era*_*nga 32

你的问题是你在命名空间中使用ISet,System.Collections.Generic但是nHibernate期望ISet Iesi.Collections.Generic.ISet<>.所以将属性定义更改为

public virtual Iesi.Collections.Generic.ISet<People> People { get; set; }
Run Code Online (Sandbox Code Playgroud)

如果您想使用.net 4 ISet<>界面,请阅读本文


eul*_*rfx 14

最新的NHibernate使用Iesi.Collections.ISet,而不是System.Collections.Generic.ISet.您可以引用Iesi程序集或使用System.Collections.Generic.ICollection:

public virtual ICollection<People> People { get; set; }
Run Code Online (Sandbox Code Playgroud)

ISet接口继承自ICollection.