Can automapper ignore destination if it is not null / only change null fields

拥有回忆 提交于 2019-12-04 07:38:42

Yes, it can, but you probably wouldn't want to go through the hassle. To do this, you're going to need to have a custom map handler for every field on the object that you want to do this (You may be able to share the custom handler among properties of the same type, but I'm not 100% sure without looking at some of my old code).

I recently solved this on my own problem using a PreCondition with Automapper 5.2.0.

CreateMap<Foo, Foo>()
  .ForAllMembers(opt => opt.Precondition(
    new Func<Foo, bool>( foo =>
      if (opt.DestinationMember == null) return true;
      return false;
    )
  ));

This looks at all destination members, and first looks to see if the destination member is null before even looking at the source member. (If it is not null, then it never looks at the source.)

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