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 >
通过“保存”将对象持久保存到数据库时,出现以下错误:
异常:无法将“Castle.Proxies.IHardwareSpecificationProxy”类型的对象转换为“Hardwaremarketplace.Model.SystemTransactions.CPUSpecification”类型。
我正在尝试找出如何解决此问题,因此我们将不胜感激。 f
I am running into an nHibernate error while saving an object.
The classes involved are:
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;}
}
}
Mapping:
<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 >
While persisting the object to database via Save, I get the following error:
Exception: Unable to cast object of type 'Castle.Proxies.IHardwareSpecificationProxy' to type 'Hardwaremarketplace.Model.SystemTransactions.CPUSpecification'.
I am trying to figure out how to resolve this so any help will be appreciated. f
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的评论,我了解到 AutoMapper 为接口属性
Specification
创建代理类型。因此,您有:
和
这是两种不兼容的类型,并且
IHardwareSpecificationProxy
对象无法转换为CPUSpecification
。您需要做的是告诉 AutoMapper 使用 CPUSpecification 类而不是动态代理。
编辑:
考虑到
SystemTransactionDTO
中有CPUSpecificationDTO
,您可以使用以下代码实现您所需要的:并且无需将
Specification
属性类型更改为CPU规格
:)。Based on your comment I understand that AutoMapper creates proxy type for interface property
Specification
.Thus you have:
and
These are two incompatible types and
IHardwareSpecificationProxy
object cannot be converted toCPUSpecification
.What you need to do is to tell AutoMapper to use
CPUSpecification
class instead of dynamic proxy.Edit:
Considering you have
CPUSpecificationDTO
insideSystemTransactionDTO
, you can achieve what you need with the following code:And no need to change
Specification
property type toCPUSpecification
:).