2016-09-26 4 views
0

このようなオブジェクトをマップすることは可能ですか?AutoMapperマッピングに追加のプロパティを渡す

Mapper.CreateMap<Source, Dest>() 
    .ConstructUsing(s => new Dest(s.first, s.second, s.Context.Options.Items["Id"])); 

Mapper.Map<Source, Dest>(src, opt => opt.Items["Id"] = 5); 

残念ながらConstructUsing方法の現在のラムダにはContexプロパティがありません。それとももっとエレガントな方法がありますか?

ありがとうございます!

を使用でき

答えて

0

cfg.CreateMap<Source, Dest>().ForMember(dest => dest.MyProperty, opt => opt.MapFrom(src => src.MySourceProperty)); 

それとも、それはより多くのニーズにフィット場合は、これを試してください:あなたが使用することもでき

cfg.CreateMap<Source, Dest>().ConvertUsing(MappingFunction); 
private Dest MappingFunction(Source source) 
{ 
    // mapping stuff 
} 

を:

cfg.CreateMap<Source, Dest>().BeforeMap(MappingFunction) 

または:

cfg.CreateMap<Source, Dest>().AfterMap(MappingFunction) 
0

旧バージョンのAutoMapperを使用しているようです。 5.xバージョンには、あなたがしようとしているのと同じように使用できるコンテキストオブジェクトが含まれています。

cfg.CreateMap<Source, Dest>() 
    .ConstructUsing((src, ctxt) => new Dest(src.first, src.second, ctxt.Options.Items["Id"])); 
関連する問題