问题
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