I am creating an Automapper mapping between my AccountEditViewModel (View Model used for editing a user) and my User (Data Model representing a User in the database). If the password field is filled in, I want to encrypt that password and store it however if it is null in the I want to keep the old password. I have tried the code below however it is wrong, model.Ignore() doesn’t return a string value. What is the best way of going about this. Can I accomplish this using the ForMember() method or do I need a custom resolver?
Mapper.CreateMap<AccountEditViewModel, User>()
.ForMember(model => model.Password, model => model.MapFrom(user => user.Password != null ? EncryptPassword(user.Password) : model.Ignore()));
Try this: