public List<Application> FindAll()
{
using (ISession NSession = SessionProvider.GetSession())
{
ICriteria CriteriaQuery =
NSession.CreateCriteria(typeof(Application));
return (List<Application>) CriteriaQuery.List<Application>();
}
}
我得到一個例外,其中應用類是以下幾點:無法投類型的對象NHibernate.Collection.Generic.PersistentGenericBag「
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]' 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和發現它非常複雜。我真的看不到這個問題。 在Application Class構造函數的最後一行出現異常。
異常發生在應用程序類的構造函數中,爲什麼會這樣呢? – FidEliO
第16行:{ 第17行:_bugs = new List(); 第18行:} –
FidEliO
可能重複的[無法轉換NHibernate.Collection.Generic類型的對象。PersistentGenericBag列出](http://stackoverflow.com/questions/1638593/unable-to-cast-object-of-type-nhibernate-collection-generic-persistentgenericbag) –