How to use AutoMapper .ForMember?
a sample implementation would be as follows:
Mapper.CreateMap<Game, GameViewModel>()
.ForMember(m => m.GameType, opt => opt.MapFrom(src => src.Type))
We need to map this property since the names of the properties of Game and GameViewModel are different - if they are the same and of the same type then it will not need a ForMember
another use of the ForMember is to Ignore Mappings
Mapper.CreateMap<Game, GameViewModel>()
.ForMember(dest => dest.Prize, opt => opt.Ignore());
In the end, I believe this turned out to be some kind of incompatibility with ReSharper.
ReSharper seems to have caused Automapper code to display incorrectly, but work just fine (even though it displays red with error messages). Uninstalling ReSharper fixed this issue completely.
This use as well as:
CreateMap<Azmoon, AzmoonViewModel>()
.ForMember(d => d.CreatorUserName, m => m.MapFrom(s =>
s.CreatedBy.UserName))
.ForMember(d => d.LastModifierUserName, m => m.MapFrom(s =>
s.ModifiedBy.UserName)).IgnoreAllNonExisting();
Try the following syntax:
Mapper
.CreateMap<Entity, EntityDto>()
.ForMember(
dest => dest.SomeDestinationProperty,
opt => opt.MapFrom(src => src.SomeSourceProperty)
);
or if the source and destination properties have the same names simply:
Mapper.CreateMap<Entity, EntityDto>();
Please checkout the relevant sections of the documentation for more details and other mapping scenarios.