简体   繁体   English

无法转换'NHibernate.Collection.Generic.PersistentGenericBag'类型的对象

[英]Unable to cast object of type '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: Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag 1[BugTracker.Model.Bug]' to type 'System.Collections.Generic.List 1[BugTracker.Model.Bug]'. System.InvalidCastException:无法将类型为'NHibernate.Collection.Generic.PersistentGenericBag 1[BugTracker.Model.Bug]' to type 'System.Collections.Generic.List对象1[BugTracker.Model.Bug]' to type 'System.Collections.Generic.List 1 [BugTracker.Model.Bug]'。

Here is the application.hbm.xml: 这是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>

I'm newbie to Nhibernate and find it very complicated. 我是Nhibernate的新手,发现它非常复杂。 I really cannot see the issue. 我真的看不出这个问题。 The exception occurs at the last line of Application Class Constructor. 异常发生在Application Class Constructor的最后一行。

Try setting your Bugs property as IList. 尝试将您的Bugs属性设置为IList。 You need to make it generic. 你需要使它通用。 Regards. 问候。

The List<>() method isn't returning a List<T> , so you can't cast it to List<T> . List<>()方法未返回List<T> ,因此您无法将其强制转换为List<T>

Instead, you should call ToList() to copy into a List<Application>() . 相反,您应该调用ToList()来复制到List<Application>() (after which you won't need a cast) (之后你不需要演员)

You Need to cast to an IList. 你需要投射到IList。 Nhiberante uses Dependency Injection and you need to use interfaces to let it do its magic. Nhiberante使用依赖注入,你需要使用接口让它发挥其魔力。

I'll tell you another time this happens that most are not aware of. 我会告诉你另一次这种情况,大多数人都不知道。 If you have two columns coming back with the same name but of a different type, you'll run into this issue. 如果您有两列具有相同名称但具有不同类型的列,则会遇到此问题。 For example, lets say you do a join based on some id and your result set looks like 例如,假设您根据某个ID进行连接,结果集如下

Id, Name, CreatedDate, Id, ExpirationDate

if the data type of the first Id column is uniqueidentifier, and the data type of the second id column is int, you're going to get 如果第一个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'.

because NHibernate will mix up the two columns as they have the same name/key. 因为NHibernate将混合两列,因为它们具有相同的名称/键。 To resolve this, just give your columns unique names via an alias: 要解决此问题,只需通过别名为列添加唯一名称:

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

and that should resolve any issues you have with the .List() method. 这应解决您对.List()方法的任何问题。

NOTE: List() is not to be confused with List in NHibernate. 注意: List()不要与NHibernate中的List混淆。 One is strongly typed and the other of course is not. 一个是强类型,另一个当然不是。 List will also auto parse the result set for you as opposed to giving you back a weakly typed IList. List还会自动为您解析结果集,而不是为您提供弱类型的IList。 In my case, I'm using just plain old List(). 就我而言,我只使用普通的旧List()。

Change your code as: 将您的代码更改为:

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

Notice that Bugs is an IList<Bug> instead of a List<Bug> . 请注意,Bugs是IList<Bug>而不是List<Bug>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM