How to Map String Literal to Destination Property

AutomapperAutomapper 2

Automapper Problem Overview


I'd like to be able to do something like this using automapper:

Mapper.CreateMap<Source, Destination>()
    .ForMember<d => d.Member, "THIS STRING">();

I'd like d.Member to always be "THIS STRING" and not be mapped from any particular member from the source model. Putting a string field in the source model with "THIS STRING" as it's value is also not an option.

Does AutoMapper support these kinds of things in any way?

Automapper Solutions


Solution 1 - Automapper

Mapper.CreateMap<Source, Destination>()
    .ForMember(dest => dest.Member, opt => opt.UseValue<string>("THIS STRING"));

Starting with version 8.0 you have to use the following:

Mapper.CreateMap<Source, Destination>()
    .ForMember(dest => dest.Member, opt => opt.MapFrom(src => "THIS STRING"));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRick EyreView Question on Stackoverflow
Solution 1 - AutomappermfantoView Answer on Stackoverflow