【发布时间】: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