Automapper, mapping to a complex object

孤者浪人 提交于 2020-01-06 14:55:45

问题


I have 2 classes i'm trying to map namely

1) Entity 2) DTO

I'm trying to map Entity.Foo to DTO.Child.Foo

Obviously the below will not work, how do I achieve this. I need to create a new instance of Child and then attach that to the Mapper and then set the Foo property but my AutoMapper skills are not that good!

Mapper.CreateMap<Entity, DTO>()
 .ForMember("Child.Foo", m => m.MapFrom(entity => entity.Foo))

回答1:


Mapper.CreateMap<Entity, DTO>()
    .ForMember(d => d.Foo, 
        o => o.ResolveUsing(s => new DTO.Child { Foo = s.Foo }))

// comment



来源:https://stackoverflow.com/questions/9498242/automapper-mapping-to-a-complex-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!