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.PersistentGenericBag1[BugTracker.Model.Bug]' to type 'System.Collections.Generic.List
1[BugTracker.Model.Bug]'.
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>
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.
Try setting your Bugs property as 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>
.
Instead, you should call ToList()
to copy into a List<Application>()
. (after which you won't need a cast)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With