I'm unit testing a method which uses automapper to map a class from my domain to a linq to sql class. Roughly, the classes and mapping are below (The SupplierEligibilityAllocated is a L2S auto generated class).
public class SupplierEligibilityTransactionByQuantity
{
public decimal Eligibility { get; private set; }
public decimal CoreValue { get; private set; }
public int? TransactionId { get; private set; }
public SupplierTransactionStatus Status { get; private set; }
public int? DebitId { get; set; }
public int ManifestId { get; private set; }
}
public partial class SupplierEligibilityAllocated
{
private int _SupplierEligibilityCreditId;
private int _ManifestId;
private System.Nullable<int> _QuantityApplied;
private System.Nullable<decimal> _AmountApplied;
private System.Nullable<decimal> _CoresReservedByAmount;
private System.DateTime _InsertDate;
private EntityRef<Manifest> _Manifest;
private EntityRef<SupplierEligibilityCredit> _SupplierEligibilityCredit;
}
private static void Map_SupplierEligibilityTransactionByQuantity_To_SupplierEligibilityAllocated()
{
Mapper.CreateMap<EligibilityTransactionByQuantity, SupplierEligibilityAllocated>()
.ForMember(dest => dest.SupplierEligibilityCreditId, opt => opt.MapFrom(src => src.TransactionId))
.ForMember(dest => dest.ManifestId, opt => opt.MapFrom(src => src.ManifestId))
.ForMember(dest => dest.QuantityApplied, opt => opt.MapFrom(src => Convert.ToInt32(src.Eligibility)))
.ForMember(dest => dest.AmountApplied, opt => opt.Ignore())
.ForMember(dest => dest.CoresReservedByAmount, opt => opt.Ignore())
.ForMember(dest => dest.InsertDate, opt => opt.MapFrom(src => DateTime.UtcNow))
.ForMember(dest => dest.Manifest, opt => opt.Ignore())
.ForMember(dest => dest.SupplierEligibilityCredit, opt => opt.Ignore());
}
When the method executes the mapping, though, it throws the following exception.
Trying to map SupplierEligibilityTransactionByQuantity to SupplierEligibilityAllocated.
Missing type map configuration or unsupported mapping.
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)
at AutoMapper.MappingEngine.Map(Object source, Type sourceType, Type destinationType)
at AutoMapper.MappingEngine.Map[TSource,TDestination](TSource source)
at AutoMapper.Mapper.Map[TSource,TDestination](TSource source)
I verified that I am creating the mapping before the test and I called Mapper.AssertConfigurationIsValid()
without any problems. I also manually did the mapping without any problems. Anybody have an idea as to what could be causing this?
If you have to do complex mapping behavior, it might be better to avoid using AutoMapper for that scenario. Reverse mapping can get very complicated very quickly, and unless it's very simple, you can have business logic showing up in mapping configuration.
AutoMapper is a great tool when used for simple conversions. When you start using more complex conversions, AutoMapper can be invaluable. For very simple conversions you could of course write your own conversion method, but why write something that somebody already has written?
Inside this article, it discusses performance and it indicates that Automapper is 7 times slower than manual mapping. This test was done on 100,000 records and I must say I was shocked.
Automatically-mapped Properties are now case sensitive · Issue #950 · AutoMapper/AutoMapper · GitHub.
It appears you are specifying the wrong type on your call to Mapper.CreateMap
Try doing something like the following:
Mapper.CreateMap<SupplierEligibilityTransactionByQuantity, SupplierEligibilityAllocated>()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With