【问题标题】:Unable to cast object of type 'System.Collections.ArrayList' to type 'System.Collections.Generic.IEnumerable'无法将“System.Collections.ArrayList”类型的对象转换为“System.Collections.Generic.IEnumerable”类型
【发布时间】:2016-05-13 16:11:34
【问题描述】:

我最近将 Windows SmartClient 解决方案从 nHibernate 2.2 升级到 4.0,并且在写入数据库时​​遇到异常。

在这段代码中抛出异常:

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; }
}

请注意,有些指定了类型,有些则没有。

一个建议here 将属性设置为IList,但我已经有了它。

可以做什么?

【问题讨论】:

  • 你有没有考虑过使用泛型?在裸露的物体周围折腾似乎很... 2003.
  • 我在原始帖子中添加了另一个具有 的代表性属性。您是否建议没有这个可能会导致问题?
  • 演员将不再存在于通用版本中。

标签: c# nhibernate


【解决方案1】:

在 NHibernate 4.0 中删除了对持久性非泛型集合的支持。改为转换为通用集合。

查看NHibernate 4.0 release notes 中的重大更改列表。

【讨论】:

    【解决方案2】:

    这里的问题可能是最新版本的 NHibernate ..即4.0

    您有以下选择。

    1) 大多数时候,新版本支持向后兼容。如果是这种情况,请查找 iSession.Save 方法的重载版本。你也可能得到非泛型。

    2) 可能新的保存方法只支持泛型类型。你的是非泛型的,即 ArrayList。如果您可以将其更改为 Ilist,那应该会有所帮助。

    3) 如果您无法控制 Ilis,那么您可以在其间编写转换器,将您的 Arraylist 转换为 Ilist,并且可以使用 Save 方法。

    希望这会有所帮助。

    【讨论】:

    • 根据您和@Robert Harvey 所说的,我正在研究#2。这可能需要一段时间。此代码已有 10 年历史。
    【解决方案3】:

    我希望我能正确理解您的问题,如果是这样,我不确定您是否需要进行空值检查。相反,您的父类应该有一个完全保存在另一个表中的位置列表。

    这是您的位置列表所在的类。

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 2014-09-14
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      相关资源
      最近更新 更多