2011-07-24 60 views
5
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構造函數的最後一行出現異常。

+0

異常發生在應用程序類的構造函數中,爲什麼會這樣呢? – FidEliO

+0

第16行:{ 第17行:_bugs = new List (); 第18行:} – FidEliO

+0

可能重複的[無法轉換NHibernate.Collection.Generic類型的對象。PersistentGenericBag列出](http://stackoverflow.com/questions/1638593/unable-to-cast-object-of-type-nhibernate-collection-generic-persistentgenericbag) –

回答

3

List<>()方法沒有返回List<T>,因此您不能將其轉換爲List<T>

相反,您應該致電ToList()複製到List<Application>()。 (之後你不需要演員陣容)

+0

仍然沒有解決它,我認爲有什麼問題在這個構造函數在我的映射文件中是不正確的! – FidEliO

7

嘗試將您的Bugs屬性設置爲IList。你需要使其通用。 此致敬禮。

+0

這是最好的答案。請參閱http://stackoverflow.com/questions/1638593/unable-to-cast-object-of-type-nhibernate-collection-generic-persistentgenericbag –

3

你需要投給IList。 Nhiberante使用依賴注入,你需要使用接口來讓它發揮它的魔力。

2

我會告訴你另一次這種情況,大多數人都沒有意識到。如果您有兩列以相同的名稱返回,但是類型不同,則會遇到此問題。例如,假設你做一個基於一些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()方法的任何問題。

注意: List()不應與NHibernate中的List混淆。一個是強類型的,另一個當然不是。 List也會自動解析結果集,而不是給你一個弱類型的IList。在我的情況下,我只使用普通的舊List()。

1

更改您的代碼爲:

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

注意,錯誤是IList<Bug>而不是List<Bug>

相關問題