How to use AutoMapper .ForMember?

.NetAutomapper

.Net Problem Overview


I am trying to set up AutoMapper to convert from Entity to DTO. I know I'm supposed to be using .ForMember() after Mapper.CreateMap<Entity, DTO>() to set up custom mappings, but this doesn't seem to be an available method.

Edit for clarification: I am not looking for a link to the documentation, which I have read, or an explanation of the basic syntax. I am using the correct syntax as described in answers and the documentation, for example:

Mapper.CreateMap<EFAddress, Address>()
      .ForMember(dest => dest.Code, opt => opt.MapFrom(src => src.Name));

If I have an invalid type name within CreateMap<> I can see "ForMember" as a valid method, mousing over shows the method signature as I would normally expect. But as soon as I give it two valid types, ForMember says it cannot resolve the symbol, as if the method is not available.

Is there some kind of constraint on the generic classes which I am not meeting?

Thanks

.Net Solutions


Solution 1 - .Net

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.

Solution 2 - .Net

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.

Solution 3 - .Net

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());

Solution 4 - .Net

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();

Solution 5 - .Net

 CreateMap<ClassRoom, ClassRoomDto>()
            .ForMember(opt => opt.StudentNumber, conf => conf.MapFrom(x => x.Student == null ? (long?)null : x.Student.StudentNumber))
            .ForMember(opt => opt.StudentFullName, conf => conf.MapFrom(x => x.Student == null ? null : x.Student.Name + " " + x.Student.Surname))
            .ReverseMap()
            .ForMember(opt => opt.Student, conf => conf.Ignore());

Solution 6 - .Net

Are you doing it like this

Mapper.CreateMap<SourceType,DestinationType>().ForMember(What ever mapping in here)

This page has some good examples

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
QuestionNelliusView Question on Stackoverflow
Solution 1 - .NetDarin DimitrovView Answer on Stackoverflow
Solution 2 - .NetNelliusView Answer on Stackoverflow
Solution 3 - .Netstack72View Answer on Stackoverflow
Solution 4 - .NetMojtaba NavaView Answer on Stackoverflow
Solution 5 - .NetZara GibbonsView Answer on Stackoverflow
Solution 6 - .NetRichard ForrestView Answer on Stackoverflow