I use AutoMapperto match the ViewModel with the model. However, I want the properties not to be displayed if the corresponding property of the source null.
My source class is as follows:
public class Source
{
public string Id { get; set; }
}
And destination class:
public class Destination
{
public Guid Id { get; set; }
}
And this is how I configured mapper:
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Source, Destination>()
.ForAllMembers(opts => opts.Condition((src, dest, srcMember) => srcMember != null));
});
I thought that mapping would mean that the properties would not be overwritten at the destination if the source code null. But apparently I'm wrong: even when Source.Idnull, it still displays, AutoMapper assigns it an empty Guid ( 00000000-0000-0000-0000-000000000000), overwriting the existing one. How can I tell AutoMapper to skip property mapping if the source is NULL?
. , Guid<->String, automapper, . , Id, null.