Galma88
Galma88

Reputation: 2546

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

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.

Upvotes: 1

Views: 1530

Answers (1)

Lucian Bargaoanu
Lucian Bargaoanu

Reputation: 3516

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

Upvotes: 5

Related Questions