How C# Automapper can set the fields to null when the string is empty

本小妞迷上赌 提交于 2019-12-11 04:16:22

问题


Is there a way to set all properties of a class, that I'm mapping, that is string.Empty should map to NULL.

 Mapper.CreateMap<TSource, TDest>();

I want that all Properties of TSource that are string Empty are mapped to NULL in TDest corresponding Properties.

I've currently not found a way to globally assign this condition without setting it up for all Properties manually.

EDIT

I need it only for a specific mapping not for all maps defined in my application.


回答1:


cfg.CreateMap<string, string>().ConvertUsing(s=>s == "" ? (string)null : s);


来源:https://stackoverflow.com/questions/53450418/how-c-sharp-automapper-can-set-the-fields-to-null-when-the-string-is-empty

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