2009-12-21 47 views
15

我正在使用automapper將一個類從我的域映射到linq到sql類的方法進行單元測試。粗略地說,類和映射在下面(SupplierEligibilityAllocated是一個L2S自動生成的類)。使用AutoMapper獲取例外

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()沒有任何問題。我也手動做了映射沒有任何問題。任何人都有一個想法,可能會造成這種情況?

+0

爲ANY1總有ü把該IgnoreDataMember ATTR的可能性,而這意味着,烏爾班外ü不能看到它,這是我的問題,這當然 – bresleveloper 2013-04-22 09:07:35

回答

9

看來您指定您的呼叫類型錯誤Mapper.CreateMap

嘗試做類似如下:

Mapper.CreateMap<SupplierEligibilityTransactionByQuantity, SupplierEligibilityAllocated>() 
+0

衛生署,我忽略了顯而易見的。我正在映射錯誤的類型,這解釋了異常以及爲什麼AssertConfigurationIsValid()沒有問題。 – JChristian 2009-12-21 21:29:47

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