1

Closed

AutoMapper does not convert the decimal? type

description

I have an entity class like this...
    public partial class OrderItem
    {
        public int OrderItemId { get; set; }
        public int OrderNumber { get; set; }
        public string isbn13 { get; set; }
        public string isbn8 { get; set; }
        public int quantityOrdered { get; set; }
        public double priceBeforeDiscount { get; set; }
        public Nullable<double> percentageDiscount { get; set; }
    
        public virtual Order Order { get; set; }
    }
and my DTO class looks like this...
public class PricingDetails
    {
        public decimal? Amount { get; set; }

        public decimal? StockPrice { get; set; }

        public decimal? DiscountPercentage { get; set; }
    }
When I try to convert the Db class to the DTO it throws the following error...
AutoMapper.AutoMapperMappingException was caught
HResult=-2146233088
Message=
Mapping types:
Int32 -> Nullable1
System.Int32 -> System.Nullable
1[[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

Destination path:
IEnumerable`1[0].ItemDetails.ItemDetails.ItemDetails0[0].PricingDetails.PricingDetails.Amount.Amount

Source value:
0
Source=AutoMapper
StackTrace:
   at Broker.Repositories.RapOrderRepository.ReadDispatchableOrders(List`1 entities) in d:\Projects\Gyldendal_OrderEngine\Broker\Features\Rap Orders\Src\Broker.DataAccess\Broker.Repositories\RapOrderRepository.cs:line 127
   at Broker.Repositories.RapOrderRepository.GetUndispatchedOrders(Guid batch) in d:\Projects\Gyldendal_OrderEngine\Broker\Features\Rap Orders\Src\Broker.DataAccess\Broker.Repositories\RapOrderRepository.cs:line 341
   at OrderBroker.Utilities.Brokers.RapOrderBroker.<ProcessOrders>d__1.MoveNext() in d:\Projects\Gyldendal_OrderEngine\Broker\Features\Rap Orders\Src\Broker.Common\OrderBroker.Utilities\Brokers\RapOrderBroker.cs:line 61
InnerException: System.InvalidCastException
   HResult=-2147467262
   Message=Specified cast is not valid.
   Source=Anonymously Hosted DynamicMethods Assembly
   StackTrace:
        at lambda_method(Closure , Object , Object )
        at AutoMapper.Impl.PropertyAccessor.SetValue(Object destination, Object value)
        at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.AssignValue(PropertyMap propertyMap, Object mappedObject, Object propertyValueToAssign)
        at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap)
   InnerException: 

Don't know where is that Int32 coming from...
Closed Jul 30, 2015 at 2:04 PM by jbogard
The project has moved to GitHub.

comments