I want to map class Input
to class Output
, but I don't want to reset properties in the Output
that are not in the Input
.
For example:
public class Input
{
public string Name { get; set; }
}
public class Output
{
public int Id { get; set; }
public string Name { get; set; }
}
public async Task Update(int id, Input input)
{
var output = await _repository.GetById(id);
////////////////////////////
// output.Id = 1; //
// output.Name = "Test"; //
////////////////////////////
if (output != null)
{
output = _mapper.Map<Output>(input);
// output.Id = 0 <------- I'd like to keep "1";
_mainRepository.Update(output);
}
}
I'd like to keep Id=1. Is it possible with AutoMapper
?
I was trying with Ignore
but it doesn't work:
public PlayerProfile()
{
CreateMap<PlayerInput, PlayerOutput>()
.ForMember(src => src.PlayerId, opt => opt.Ignore())
.ForMember(src => src.Name, opt => opt.MapFrom(src => src.Name));
}