C# 使用 AutoMapper 获取异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1942691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting an exception with AutoMapper
提问by JChristian
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).
我正在对一种使用 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());
}
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?
我确认我在测试之前创建了映射,我打电话Mapper.AssertConfigurationIsValid()
没有任何问题。我还手动进行了映射,没有任何问题。任何人都知道可能导致这种情况的原因?
采纳答案by The Matt
It appears you are specifying the wrong type on your call to Mapper.CreateMap
看来您在调用时指定了错误的类型 Mapper.CreateMap
Try doing something like the following:
尝试执行以下操作:
Mapper.CreateMap<SupplierEligibilityTransactionByQuantity, SupplierEligibilityAllocated>()
回答by atik sarker
If any one using Mapper.Map()
check your mapping class & table/store procedure properly.
如果有人使用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);
}