Получение исключения с AutoMapper - PullRequest
15 голосов
/ 22 декабря 2009

Я занимаюсь модульным тестированием метода, который использует automapper для отображения класса из моего домена в класс linq to sql. Грубо говоря, классы и сопоставления приведены ниже (SupplierEligibilityAllocated является автоматически генерируемым классом L2S).

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());
}

Когда метод выполняет отображение, он выдает следующее исключение.

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)  

Я проверил, что я создаю сопоставление перед тестом, и позвонил Mapper.AssertConfigurationIsValid() без проблем. Я также вручную сделал сопоставление без каких-либо проблем. У кого-нибудь есть идеи относительно того, что может быть причиной этого?

Ответы [ 2 ]

9 голосов
/ 22 декабря 2009

Похоже, вы указываете неправильный тип при вызове Mapper.CreateMap

Попробуйте сделать что-то вроде следующего:

Mapper.CreateMap<SupplierEligibilityTransactionByQuantity, SupplierEligibilityAllocated>()
0 голосов
/ 16 июня 2014

Если кто-либо использует Mapper.Map(), проверьте ваш класс отображения и процедуру таблицы / хранилища правильно.

public static CustomerLedgerViewModel ToModel(this DJBL_tblCustomerCurrentLedger obj)
{
    return Mapper.Map<DJBL_tblCustomerCurrentLedger, CustomerLedgerViewModel>(obj);
}

public static DJBL_tblCustomerCurrentLedger ToEntity(this CustomerLedgerViewModel model)
{
    return Mapper.Map<CustomerLedgerViewModel, DJBL_tblCustomerCurrentLedger>(model);
} 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...