自动映射器-多对象源和一个目标 [英] Automapper - Multi object source and one destination

查看:69
本文介绍了自动映射器-多对象源和一个目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自动映射器来映射多个对象(将db类转换为ui对象).

I am using auto mapper to map multiple objects (db class into ui objects).

地图1:

Mapper.CreateMap<sourceone, destination>().ForMember(sss => sss.one, m => m.MapFrom(source => source.abc));

地图2:

Mapper.CreateMap<sourcetwo, destination>().ForMember(sss => sss.two, m => m.MapFrom(source => source.xyz));

destination d = new destination();

//地图1

d = AutoMapper.Mapper.Map<sourceone, destination>(sourceone);

//地图2

d = AutoMapper.Mapper.Map<sourcetwo, destination>(sourcetwo);

一旦我调用地图2",使用地图1填充的值就会丢失..(即destination.one变为空).我该如何解决?

Once I make call to the 'Map 2', the values that are populated using Map 1 are lost.. (i.e destination.one is becoming empty). How do I fix this?

推荐答案

Map具有包含源对象和目标对象的重载:

Map has an overload that takes a source and destination object:

d = AutoMapper.Mapper.Map<sourceone, destination>(sourceone);

/* Pass the created destination to the second map call: */
AutoMapper.Mapper.Map<sourcetwo, destination>(sourcetwo, d);

这篇关于自动映射器-多对象源和一个目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆