C# AutoMapper – Set default value in dest property if not exist in source

I have:

Class A
{
    public string FirstName { get; set; }
}

Class B
{
    public string FirstName { get; set; }
    public Guid RequestId { get; set; }
}

I want to map from A to B, since "A" doesn't have RequestId I want to set it to Guid.NewGuid()

I tried this code:

var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<A, B>().ForMember(m => m.RequestId, o => Guid.NewGuid());
            
            });
            _mapper = config.CreateMapper();

But I'm still getting empty Guid in RequestId.



Read more here: https://stackoverflow.com/questions/64900494/c-sharp-automapper-set-default-value-in-dest-property-if-not-exist-in-source

Content Attribution

This content was originally published by cheziHoyzer at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: