nHibernate 异常:无法转换类型的对象

发布于 2024-10-16 02:25:24 字数 1250 浏览 1 评论 0原文

我在保存对象时遇到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠佳期 2024-10-23 02:25:24

根据您的评论,我了解到 AutoMapper 为接口属性 Specification 创建代理类型。
因此,您有:

public class CPUSpecification : IHardwareSpecification { }

public class IHardwareSpecificationProxy : IHardwareSpecification{ }

这是两种不兼容的类型,并且 IHardwareSpecificationProxy 对象无法转换为 CPUSpecification

您需要做的是告诉 AutoMapper 使用 CPUSpecification 类而不是动态代理。

编辑:
考虑到 SystemTransactionDTO 中有 CPUSpecificationDTO,您可以使用以下代码实现您所需要的:

Mapper.CreateMap<SystemTransactionDTO, SystemTransaction>();
Mapper.CreateMap<CPUSpecificationDTO, CPUSpecification>();
Mapper.CreateMap<CPUSpecificationDTO, IHardwareSpecification>()
    .ConvertUsing(dto => Mapper.Map<CPUSpecificationDTO, CPUSpecification>(dto));

并且无需将 Specification 属性类型更改为 CPU规格:)。

Based on your comment I understand that AutoMapper creates proxy type for interface property Specification.
Thus you have:

public class CPUSpecification : IHardwareSpecification { }

and

public class IHardwareSpecificationProxy : IHardwareSpecification{ }

These are two incompatible types and IHardwareSpecificationProxy object cannot be converted to CPUSpecification.

What you need to do is to tell AutoMapper to use CPUSpecification class instead of dynamic proxy.

Edit:
Considering you have CPUSpecificationDTO inside SystemTransactionDTO, you can achieve what you need with the following code:

Mapper.CreateMap<SystemTransactionDTO, SystemTransaction>();
Mapper.CreateMap<CPUSpecificationDTO, CPUSpecification>();
Mapper.CreateMap<CPUSpecificationDTO, IHardwareSpecification>()
    .ConvertUsing(dto => Mapper.Map<CPUSpecificationDTO, CPUSpecification>(dto));

And no need to change Specification property type to CPUSpecification:).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文