无法强制转换'nhibernate . collection . generic . persistentgen

本文关键字:nhibernate collection generic persistentgen 转换 | 更新日期: 2023-09-27 18:03:26

public List<Application> FindAll()
{
    using (ISession NSession = SessionProvider.GetSession())
    {
        ICriteria CriteriaQuery =
            NSession.CreateCriteria(typeof(Application));
        return (List<Application>) CriteriaQuery.List<Application>();
    }
}

我得到一个异常,其中应用程序类如下:

public class Application
{
     private string _name;
     private Developer _developer;
     private int _id;
     private List<Bug> _bugs;
    public Application()
    {
    }
    public virtual int ApplicationId
    {
        get { return _id; }
        set { _id = value; }
    }
    public virtual Developer Developer
    {
        get { return _developer; }
        set { _developer = value; }
    }
    public virtual string Name
    {
        get { return _name; }
        set { _name = value; }
    }
    public virtual List<Bug> Bugs
    {
        get { return _bugs; }
        set { _bugs = value; }
    }
}

系统。InvalidCastException:无法强制转换类型为'NHibernate.Collection.Generic.PersistentGenericBag 1[BugTracker.Model.Bug]' to type 'System.Collections.Generic.List 1[BugTracker.Model.Bug]'的对象。

application.hbm.xml:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="BugTracker.Model"
                   assembly="BugTracker">
  <class name="Application" table="Applications" lazy="false">
    <id name="ApplicationId" column ="ApplicationId" type="int" unsaved-value ="0">
      <generator class ="native"></generator>
    </id>
    <property name ="Name" column="Name"/>
    <component access ="field.camelcase-underscore" name ="Developer"
               class="Developer">
      <property access ="field.camelcase-underscore" 
                column ="DeveloperFirstName" name="FirstName"/>
      <property access ="field.camelcase-underscore" 
                column="DeveloperLastName" name="LastName"/>
    </component>
    <bag cascade="all-delete-orphan"
          inverse ="true"
          name ="Bugs"
          lazy="false"
          access ="field.camelcase-underscore">
      <key column ="ApplicationId"/>
      <one-to-many class ="Bug"/>
    </bag>
  </class>
</hibernate-mapping>

我是Nhibernate的新手,发现它非常复杂。我真的看不出有什么问题。异常发生在应用程序类构造函数的最后一行。

无法强制转换'nhibernate . collection . generic . persistentgen

尝试将Bugs属性设置为IList。你需要让它变得通用。问候。

List<>()方法不返回List<T>,因此您不能将其强制转换为List<T>

相反,您应该调用ToList()来复制到List<Application>()。(之后就不需要cast了)

您需要强制转换为illist。nhibernate使用依赖注入,你需要使用接口来让它发挥它的魔力。

我会告诉你下一次发生这种情况,大多数人都没有意识到。如果返回的两列名称相同但类型不同,就会遇到这个问题。例如,假设基于某个id执行连接结果集为

Id, Name, CreatedDate, Id, ExpirationDate

如果第一个Id列的数据类型是uniqueidentifier,第二个Id列的数据类型是int,你将得到

Input string 'f49f503d-70d5-4fbb-8aa2-a0bd0113ff4d' was not in
the correct format. ----> System.InvalidCastException : 
Unable to cast object of type 'System.Guid' to type 'System.IConvertible'.

,因为NHibernate会混淆这两列,因为它们有相同的名称/键。要解决这个问题,只需通过别名为列指定唯一的名称:

select Id as IDOne, Name, CreatedDate, Id as IDTwo, ExpirationDate
from Table1 t
join Table2 t2
on t.Name = t2.Name

,这应该可以解决你使用。list()方法时遇到的任何问题。

注意: List()不要与NHibernate中的List混淆。一个是强类型的,另一个当然不是。List还将自动为您解析结果集,而不是返回一个弱类型的illist。在我的例子中,我只是使用普通的List()。

将代码更改为:

public virtual IList<Bug> Bugs
{
    get { return _bugs; }
    set { _bugs = value; }
}

注意Bugs是IList<Bug>而不是List<Bug>