使用AutoMapper获取异常

时间:2009-12-21 21:19:45

标签: c# automapper

我正在测试一个方法,该方法使用automapper将类从我的域映射到linq到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 个答案:

答案 0 :(得分:9)

您在调用Mapper.CreateMap

时似乎指的是错误的类型

尝试执行以下操作:

Mapper.CreateMap<SupplierEligibilityTransactionByQuantity, SupplierEligibilityAllocated>()

答案 1 :(得分:0)

如果使用Mapper.Map()的任何人检查您的地图类&amp;表/存储过程正确。

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