我在保存对象时遇到nHibernate错误。nHibernate异常:无法投射类型为
涉及的类别是:
interface IHardwareSpecification
{
//fields and methods
}
public class CPUSpecification : IHardwareSpecification
{
//fields and methods
}
public class SystemTransaction
{
//Bunch of other fields
private IHardwareSpecification _specs;
public virtual IHardwareSpecification Specification
{
get { return _specs; }
set { _specs = value;}
}
}
映射:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" ...>
<class name="SystemTransaction" table="SystemTransactions" lazy="false">
<component access="field.camelcase-underscore" name="Specification"
class="HardwareMarketplace.Model.CPUSpecification">
<property access="field.camelcase-underscore" column="Specification_Rate"
name="Rate"/>
...
</component>
</class>
</hibernate-mapping >
虽然通过保存持久化对象到数据库中,我得到以下错误:
Exception: Unable to cast object of type 'Castle.Proxies.IHardwareSpecificationProxy' to type 'Hardwaremarketplace.Model.SystemTransactions.CPUSpecification'.
我我试图找出如何解决这个问题,所以任何帮助不胜感激。 f
@Diego CPUSpecification是一个值对象,因此没有映射。我没有IHardwareSpecification的映射。 – fjxx 2011-02-04 20:53:06
到目前为止,我注意到的唯一错误是该字段被命名为“_specs”,但映射中的名称是“Specification”。该字段应根据您的访问设置命名为“_specification”。否则我不能重现你的问题。更多信息可能会有帮助 – AlexD 2011-02-04 21:06:44