2016-05-13 39 views
1

最近Windows SmartClientソリューションをnHibernate 2.2から4.0にアップグレードし、dbに書き込む際に例外が発生しています。'System.Collections.ArrayList'型のオブジェクトを型 'System.Collections.Generic.IEnumerable'にキャストできません

例外は、このコードでスローされます。

this.session.Save(this.Location); // NHibernate.ISession 
tx.Commit(); // exception thrown here 

例外は次のとおりです。

NHibernate.dll System.InvalidCastExceptionの中に 'System.InvalidCastExceptionの': 型のオブジェクトをキャストすることができません'System.Collections.ArrayList' と入力して 'System.Collections.Generic.IEnumerable`1 [System.Object]'と入力します。いくつかのタイプが指定されており、一部にはないことを

protected System.Collections.IList locationList; 
public virtual System.Collections.IList AssociatedLocationList 
{ 
    get 
    { 
     if (this.locationList == null) 
     { 
      this.locationList = new System.Collections.ArrayList(); 
     } 
     return this.locationList; 
    } 
    set { this.locationList = value; } 
} 
protected System.Collections.Generic.IList<Inspection> inspectionList; 
public virtual System.Collections.Generic.IList<Inspection> InspectionList 
{ 
    get 
    { 
     if (this.inspectionList == null) 
     { 
      this.inspectionList = new System.Collections.Generic.List<Inspection>(); 
     } 

     return this.inspectionList; 
    } 
    set { this.inspectionList = value; } 
} 

注:

が保存されているオブジェクトで複数のリストがあり、ここではカップル代表的なものです。

提案の1つhereは、IListにプロパティが設定されていますが、すでにそれを持っています。

何ができますか?

+1

ここでジェネリック薬を使用することを検討しましたか?裸の物体の周りを投げることは非常に... 2003. –

+0

私は元の投稿にを持つ別の代表的なプロパティを追加しました。これがないと問題を引き起こす可能性があることを示唆していますか? –

+0

キャストはジェネリック版には存在しなくなりました。 –

答えて

2

NHibernate 4.0では、永続的で非ジェネリックなコレクションのサポートが削除されました。代わりに汎用コレクションに変換してください。

NHibernate 4.0 release notesの急な変更のリストを参照してください。

1

私はあなたの質問を正しく理解していると思いますが、もしそうなら、私はあなたのヌルチェックをやっている必要があるかどうか確信していません。代わりに、親クラスには別のテーブルに保持されている場所のリストが必要です。

これは、あなたのロケーションリストがあるクラスです。

public class Parent 
{ 
    public virtual Guid Id { get; set; } 
    public virtual IList<Location> Locations { get; set; } 

    //This is the mapping for this class. 
    public class ParentMapping : ClassMap<Parent> 
    { 
     Id(x => x.Id).GeneratedBy.Guid(); 

     //This is what relates your location list to this parent. 
     //Notice that in the Location object below, 
     //there's a Owner property which will point back to here. 
     HasMany(x => x.Locations).Cascade.All(); 
    } 
} 

そして、これはあなたの場所を定義するクラスです。

public class Location 
{ 
    public virtual Guid Id { get; set; } 
    public virtual Parent Owner { get; set; } 
    public virtual string SomeProperty { get; set; } 

    //This is the mapping for this class. 
    public class LocationMapping : ClassMap<Location> 
    { 
     Id(x => x.Id).GeneratedBy.Guid(); 

     Map(x => x.SomeProperty); 

     //This will relate our property back to the parent. 
     References(x => x.Owner); 
    } 
} 
2

ここでの問題は、NHibernateはを..i.eの最新バージョンである可能性があります。 4.0

次のオプションがあります。

1)ほとんどの場合、新しいバージョンは下位互換性をサポートします。そのような場合は、iSession.Saveメソッドのオーバーロードバージョンを探します。あなたも非ジェネリックになるかもしれません。

2)おそらく新しい保存メソッドはジェネリック型のみをサポートします。あなたのものは一般的ではない、つまりArrayListです。それをIlist <>に変更できる場合は、それが役立つはずです。

3)Ilisを制御できない場合、ArraylistをIlist <に変換できるコンバータを作成して、Saveメソッドで動作させることができます。

これが役に立ちます。

+0

あなたと@Robert Harveyが言ったことに基づいて、私は#2に取り組んでいます。これはしばらく時間がかかることがあります。このコードは10歳です。 –

関連する問題