AutoMapper ForMember Causing Fits

Frist, absolutely a fan of AutoMapper. The other day added AutoMapper 3.2.0 to my ASP.NET MVC project to handle mapping between my EF 6.0 entities and ViewModels. Initially all was fine, created a simple straight mapping from Entity to ViewModel, meaning no ForMember or ForSourceMember. Directed a fellow developer to create Mapper Profiles for remaining Entities and ViewModels using the ForMember and ForSourceMember to handle mappings not following naming conventions. The next day he lets me know the ForMember is not working and won’t compile. My initial response, must be using wrong types. A quick code review, a few cringes and chastings for not using ReSharper I quickly realized that was not the case.

AutoMapper ForMember Destination Expression
1
2
Mapper.CreateMap<LookupAdditionalViewModel, LookupAdditional>()
  .ForMember(d => d.LookupAdditionalValues, option => option.Ignore());

A couple hours before I committed a large refactoring of how EF was leveraged in the application. I was pretty sure my refactoring was not the issue but with any large refactoring there is a possibility. I started playing with the code, ReSharper was yelling at me no matter what I tried until I leveraged the type as a string name.

AutoMapper ForMember Destination String Type
1
2
Mapper.CreateMap<LookupAdditionalViewModel, LookupAdditional>()
  .ForMember("LookupAdditionalValues", option => option.Ignore());

ReSharper could not poke the expression until I passed the type as a string without using an expression. This allowed me to rule out my refactoring as an issue and pointed to an issue with AutoMapper. I was flabbergasted! I found AutoMapper Issue 381 upon initial read pointed to R# and Automapper. However, I knew from my scenario this was not the case, in fact seemed to be an AutoMapper issue. After reading AutoMapper Issue 381 in it’s entirety the conclusion was AutoMapper was indeed the source of the issue and a patch release was made to resolve the behavior.

Solution

Upgrade to AutoMapper 3.2.1-ci1000 or AutoMapper 3.2.1-ci1001 will resolve the issue. Thanks to AutoMapper team for fixing the issue quickly!

Update

AutoMapper 3.2.1 was released April 25th.

Comments