传递对象到AutoMapper映射 [英] Pass Objects to AutoMapper Mapping
本文介绍了传递对象到AutoMapper映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我与AutoMapper工作,有的为实体值映射到在我的当前方法的变量。我试图谷歌,但无济于事。我可以通过一组键值对或一个物体或东西到我的映射有它使用这些值?
后修改映射样本
//注释变量是评论类实例
VAR imageComment = AutoMapper.Mapper.Map< Data.ImageComment>(注解);
//我想在imageId传递所以我没有映射后手动添加
imageComment.ImageId = imageId;
解决方案
AutoMapper处理这个键值对的场景开箱。
Mapper.CreateMap<信源,目的地>()
.ForMember(D => d.Foo,选择=> opt.ResolveUsing(RES => res.Context.Options.Items [富));
然后在运行时:
Mapper.Map<信源,目的地>(SRC,选择=> opt.Items [富] =酒吧);
详细有点挖成的背景项目,但你去那里。
I am working with AutoMapper and some of the values for the entity being mapped to are variables in my current method. I have tried to Google it but to no avail. Can I pass a set of KeyValue Pairs or an object or something to my mapping to have it use those values?
Sample of Post Mapping Modification
//comment variable is a Comment class instance
var imageComment = AutoMapper.Mapper.Map<Data.ImageComment>(comment);
//I want to pass in imageId so I dont have to manually add it after the mapping
imageComment.ImageId = imageId;
解决方案
AutoMapper handles this key-value pair scenario out of the box.
Mapper.CreateMap<Source, Dest>()
.ForMember(d => d.Foo, opt => opt.ResolveUsing(res => res.Context.Options.Items["Foo"]));
Then at runtime:
Mapper.Map<Source, Dest>(src, opt => opt.Items["Foo"] = "Bar");
A bit verbose to dig into the context items but there you go.
这篇关于传递对象到AutoMapper映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文