FindAll() { using (ISession NSe...">

Unable to pass an 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 get an exception in which the application class is as follows:

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: 1[BugTracker.Model.Bug]' to type 'System.Collections.Generic.ListCan not throw an object of type "NHibernate.Collection.Generic.PersistentGenericBag 1 [BugTracker.Model.Bug]".

Here is the application.hbm.xml application:

<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 am new to Nhibernate and find this very difficult. I really don't see the problem. An exception occurs in the last line of the Application Class Constructor.

+5
source share
5 answers

Try setting the Bugs property to IList. You must make it common. Regards.

+7
source

List<>() List<T>, List<T>.

ToList() List<Application>(). ( )

+3

IList. Nhiberante Dependency Injection, , .

+3

, , . , , , . , , id,

Id, Name, CreatedDate, Id, ExpirationDate

Id , 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'.

NHibernate , /. , :

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

, .List().

. () NHibernate. , , , . , IList. List().

+2

:

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

Please note that errors are IList<Bug>instead List<Bug>.

+1
source

All Articles