I want to convert an array of ids from a dto to a list of related objects using AutoMapper, however, the mapping only works one way.
The current setup is the following:
public class Group
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Location> Locations { get; set; }
}
public class GroupDto
{
public int id { get; set; }
public string name { get; set; }
public int[] locationIds { get; set; }
}
And the code in the GroupProfile:
CreateMap<Group, GroupDto>()
.ForMember(dest => dest.id, opt => opt.MapFrom(src => src.Id))
.ForMember(dest => dest.name, opt => opt.MapFrom(src => src.Name))
.ForMember(dest => dest.locationIds, opt =>
opt.MapFrom(src => src.Locations.Select(l => l.Id).ToArray()));
This code works when I try to convert a Group to a GroupDto, but not when I try it the other way around. The following error occurs:
An unhandled exception has occurred while executing the request. AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types: GroupDto -> Group
Also, when I do it without the AutoMapper like below, it just works:
Group group = new Group
{
Name = groupDto.name,
Locations = _context.Locations
.Where(l => groupDto.locationIds.Contains(l.Id))
.ToList()
};
Help is appreciated!