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
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
:).