5
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.List1[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.

FidEliO
  • 875
  • 3
  • 14
  • 24
  • exception happens at the constructor of the application class, why is that so? – FidEliO Jul 24 '11 at 19:34
  • Line 16: { Line 17: _bugs = new List(); Line 18: } – FidEliO Jul 24 '11 at 19:40
  • possible duplicate of [Unable to cast object of type NHibernate.Collection.Generic.PersistentGenericBag to List](http://stackoverflow.com/questions/1638593/unable-to-cast-object-of-type-nhibernate-collection-generic-persistentgenericbag) – Mauricio Scheffer Jul 25 '11 at 01:30

5 Answers5

7

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

Fricco
  • 71
  • 1
  • 2
  • This is the best answer. See http://stackoverflow.com/questions/1638593/unable-to-cast-object-of-type-nhibernate-collection-generic-persistentgenericbag – Tom Bushell Jan 11 '12 at 16:40
3

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, 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

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. 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.

NOTE: List() is not to be confused with List in NHibernate. 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. In my case, I'm using just plain old List().

A-Dubb
  • 1,670
  • 2
  • 17
  • 22
3

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)

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Still not fixing it, I think there is something wrong in the constructor which is in a way not right with my mapping file! – FidEliO Jul 24 '11 at 19:18
3

You Need to cast to an IList. Nhiberante uses Dependency Injection and you need to use interfaces to let it do its magic.

Idan Bauer
  • 31
  • 1
1

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>.

JLRishe
  • 99,490
  • 19
  • 131
  • 169
Trouble
  • 11
  • 2