Automapper does not map List<T> to List<T> while mapping List

1

I'm using automapper with CQRS pattern. Below is my class which takes input from .net core API. The API takes collection as input and I'm sending collection in my Mediatr Command object. In Mediatr command, I'm mapping source collection to destination collection and while doing mapping, I'm getting following exception:

AutoMapper.AutoMapperMappingException
  HResult=0x80131500
  Message=Error mapping types.

Inner Exception 1:
AutoMapperMappingException: Missing type map configuration or unsupported mapping.

I'm using follwing code for mapping:

var insertData = _mapper.Map<List<Source>, List<Destination>>(request.Data.ToList());

In my class, I have following:

public class Source: ICustomMapping
    {
        public int? Prop1 { get; set; }
        public string Prop2 { get; set; }
        public void CreateMappings(Profile configuration)
        {
            configuration.CreateMap<Destination, Source>()
                .ForMember(dto => dto.Prop1 , opt => opt.MapFrom(p => p.Prop1 ))
                .ForMember(dto => dto.Prop2, opt => opt.MapFrom(p => p.Prop2))
                ;
        }
    }

This mapping works flawlessly when I have single object in both ways (forward and reverse). Now I need to pass collection of object for processing and save destination collection data in to database.

automapper
asked on Stack Overflow Aug 19, 2019 by pjsoni

1 Answer

0

After looking in to the documentation I realize that I do not have the reverse mapping.

public void CreateMappings(Profile configuration)
        {
            configuration.CreateMap<Destination, Source>()
                .ForMember(dto => dto.Prop1 , opt => opt.MapFrom(p => p.Prop1 ))
                .ForMember(dto => dto.Prop2, opt => opt.MapFrom(p => p.Prop2))
                .ReverseMap();
        }

The ReverseMap() I was missing.

Thanks

answered on Stack Overflow Aug 20, 2019 by user11949123

User contributions licensed under CC BY-SA 3.0