2009-12-21 12 views
15

Jestem jednostką testującą metodę, która używa automappera do odwzorowania klasy z mojej domeny na klasę linq na sql. Z grubsza, klasy i mapowanie są poniżej (The SupplierEligibilityAllocated jest automatycznie generowaną klasą L2S).Wyjątek z AutoMapperem

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

Kiedy metoda wykonuje odwzorowanie, generuje następujący wyjątek.

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 sprawdzeniu, że tworzę mapowanie przed badaniem i zadzwoniłem Mapper.AssertConfigurationIsValid() bez żadnych problemów. Również ręcznie wykonałem mapowanie bez żadnych problemów. Ktoś ma pomysł, co może być przyczyną tego?

+0

dla any1 zawsze istnieje możliwość u umieścić attr IgnoreDataMember, a to oznacza, że ​​poza ur klasie u cant go zobaczyć, to był mój problem z tym ex – bresleveloper

Odpowiedz

9

Wydaje jesteś podając niewłaściwy typ na rozmowy do Mapper.CreateMap

Spróbuj zrobić coś jak następuje:

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

Doh, przeoczyłem oczywiste. Mapowałem niewłaściwy typ, który wyjaśnia wyjątek i dlaczego AssertConfigurationIsValid() nie miał problemu. – JChristian

0

Jeśli ktoś używając Mapper.Map() sprawdzić klasy mapowanie procedury/sklep & stołowego prawidłowo .

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); 
} 
Powiązane problemy