使用AutoMapper时出现异常

16

我正在单元测试一个使用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()而没有任何问题。 我也手动进行了映射而没有遇到任何问题。 有人知道可能是什么原因吗?


对于任何一个变量,你都可以使用IgnoreDataMember属性来隐藏它,这意味着在类的外部是看不到它的。这就是我在这个例子中遇到的问题。 - bresleveloper
2个回答

10

看起来你在调用Mapper.CreateMap时指定了错误的类型。

尝试像下面这样做:

Mapper.CreateMap<SupplierEligibilityTransactionByQuantity, SupplierEligibilityAllocated>()

我太粗心了,忽略了显而易见的错误。我正在映射错误的类型,这解释了异常的原因,也解释了为什么AssertConfigurationIsValid()没有出现问题。 - JChristian

0

如果有人使用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);
} 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接