102

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

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
Nellius
  • 3,024
  • 4
  • 20
  • 21
  • 2
    http://automapper.codeplex.com/wikipage?title=Flattening&referringTitle=Home Is likely the best documentation for what you are looking to do. Don't forget the generic parameters on the CreateMap call. – Travis Aug 08 '11 at 16:11
  • 2
    It should be noted that the static `Mapper.CreateMap()` methods referenced in all the answers below are now (2016) [marked obsolete](https://lostechies.com/jimmybogard/2016/01/21/removing-the-static-api-from-automapper/). – Ben Ripley May 25 '16 at 16:44

6 Answers6

159

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.

m4tt1mus
  • 1,642
  • 14
  • 24
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    My issue is that when trying to use "ForMember" in that context I simply get "Cannot resolve symbol 'ForMember'" as if the method doesn't exist – Nellius Aug 08 '11 at 16:15
  • @Nellius, you haven't shown your *non-working* code. I have shown a working example. Make sure you have both the source and destination generic classes defined, that you have referenced AutoMapper inside your project and the syntax shown in the documentation (and in my answer) will work. – Darin Dimitrov Aug 08 '11 at 16:17
  • 6
    Stupid question, what opt means? – riadh gomri Jan 03 '14 at 13:32
  • @riadhgomri option/options for the member – andrewb Oct 26 '15 at 05:02
19

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.

Nellius
  • 3,024
  • 4
  • 20
  • 21
  • 2
    What version of ReSharper? What Version of AutoMapper? – Colin Pear Dec 03 '12 at 21:02
  • 2
    I think this problem affects AutoMapper and ReSharper 8.x in VisualStudio 2010. See https://github.com/AutoMapper/AutoMapper/issues/381 – dthrasher Nov 14 '13 at 02:04
  • the same for me with re# 8.2 but with vs 2012, with 7.0 worked fine. – Carlos Guillermo Bolaños Lopez May 29 '14 at 20:27
  • why isn't the answer with 51 up votes not the selected answer? ridiculous. – Chris Hawkes Dec 08 '15 at 21:23
  • 7
    Because it didn't answer the question I asked. My question was about the methods not appearing to be recognised in visual studio, when using the correct syntax and referencing everything correctly. This turned out to be a bug related to resharper. The other answer does answer a very similar question to the one I asked, but it was a completely incorrect answer to the one I actually asked. – Nellius Dec 09 '15 at 09:39
10

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());
stack72
  • 8,198
  • 1
  • 31
  • 35
1

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();
Mojtaba Nava
  • 858
  • 7
  • 17
0
 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());
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 25 '21 at 22:22
-8

Are you doing it like this

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

This page has some good examples

Richard Forrest
  • 3,567
  • 2
  • 23
  • 32