简体   繁体   中英

How to assign a new variable without mapping using Automapper

I want to set PasswordSalt directly, without mapping it from any of the properties available in the DTO.

public class SaveUserRequestDTOProfile: Profile
{
    public SaveUserRequestDTOProfile()
    {
        var salt = GenerateSalt();

        .ForMember(m => m.Username, map => map.MapFrom(s => s.Username))
        .ForMember(m => m.PasswordSalt, salt); // this doesn't work

    }
}

Thanks in Advance!

You would map it in the same way as Username, only you would specify the value you created, for instance

public class SaveUserRequestDTOProfile: Profile
{
  public SaveUserRequestDTOProfile()
  {
    var salt = GenerateSalt();

    .ForMember(m => m.Username, map => map.MapFrom(s => s.Username))
    .ForMember(m => m.PasswordSalt, map => map.MapFrom(_ => salt));

  }
}

You can just specify any value in the MapFrom method. It's not a requirement that the value must actually come from the source object:

.ForMember(m => m.PasswordSalt, map => map.MapFrom(s => salt));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM