-1

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));
    }
Ish Thomas
  • 2,270
  • 2
  • 27
  • 57
  • Does this answer your question? [Automapper: Update property values without creating a new object](https://stackoverflow.com/questions/2374689/automapper-update-property-values-without-creating-a-new-object) – devNull Feb 29 '20 at 21:19

1 Answers1

-1

Try using the different overload of Map method.

_mapper.Map<Input, Output>(input, output); or _mapper.Map(input, output); to map to an existing one.

Voodoo
  • 1,550
  • 1
  • 9
  • 19