public List<Application> FindAll()
{
using (ISession NSession = SessionProvider.GetSession())
{
ICriteria CriteriaQuery =
NSession.CreateCriteria(typeof(Application));
return (List<Application>) CriteriaQuery.List<Application>();
}
}
Tôi nhận được một ngoại lệ, trong đó lớp ứng dụng như sau:Không thể cast đối tượng của loại '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: Không thể bỏ đối tượng của nhập 'NHibernate.Collection.Generic.PersistentGenericBag 1[BugTracker.Model.Bug]' to type 'System.Collections.Generic.List
1 [BugTracker.Model.Bug]'.
Đây là 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>
Tôi newbie để Nhibernate và thấy nó rất phức tạp. Tôi thực sự không thể nhìn thấy vấn đề. Trường hợp ngoại lệ xảy ra ở dòng cuối cùng của Lớp Ứng dụng Constructor.
ngoại lệ xảy ra tại hàm tạo của lớp ứng dụng, tại sao vậy? – FidEliO
Dòng 16: { Dòng 17: _bugs = new List(); Dòng 18:} –
FidEliO
có thể trùng lặp của [Không thể truyền đối tượng thuộc loại NHibernate.Collection.Generic.PersistentGenericBag vào danh sách] (http://stackoverflow.com/questions/1638593/unable-to-cast-object-of-type-nhibernate-collection-generic-persistentgenericbag) –