无法投类型的对象'NHibernate.Collection.Generic.PersistentGenericBag“ [英] Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag'

查看:532
本文介绍了无法投类型的对象'NHibernate.Collection.Generic.PersistentGenericBag“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

        return (List<Application>) CriteriaQuery.List<Application>();

    }
}



我收到一个异常,其中应用类是以下内容:

I am getting an exception in which application class is the following:

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; }
    }
}



System.InvalidCastException:无法投类型的对象NHibernate.Collection.Generic.PersistentGenericBag 1 [BugTracker.Model.Bug]'键入'System.Collections.Generic.List 1 [BugTracker.Model.Bug]。

System.InvalidCastException: Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag1[BugTracker.Model.Bug]' to type 'System.Collections.Generic.List1[BugTracker.Model.Bug]'.

下面是application.hbm.xml:

Here is the 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和发现它很复杂。我真的看不出问题。
中的例外发生在应用程序类的构造函数的最后一行。

I'm newbie to Nhibernate and find it very complicated. I really cannot see the issue. The exception occurs at the last line of Application Class Constructor.

推荐答案

尝试设置你的虫虫财产的IList。你需要使它通用。
问候。

Try setting your Bugs property as IList. You need to make it generic. Regards.

这篇关于无法投类型的对象'NHibernate.Collection.Generic.PersistentGenericBag“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆