I am mapping with automapper 5.2 from Dto to Model and from Model to Dto. But the problem I have, that have 0 elements when I do the mapping. The two entities are the same.
AutoMapperConfiguration.cs
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.CreateMap<PaisDto, Pais>().ReverseMap();
x.CreateMap<List<PaisDto>, List<Pais>>().ReverseMap();
x.CreateMap<Pais, PaisDto>().ReverseMap();
x.CreateMap<List<Pais>, List<PaisDto>>().ReverseMap();
});
}
}
PaisService.cs
public IEnumerable<PaisDto> GetAll()
{
AutoMapperConfiguration.Configure();
List<PaisDto> dto = new List<PaisDto>();
IEnumerable<Pais> paises = _paisRepository.GetPaisAll();
dto = Mapper.Map<List<Pais>,List<PaisDto>>(paises.ToList());
return dto.ToList();
}
what can happen?
Not sure if this is what you are looking for but you can create a map for the single pais/paisDto
and when you want to map a list you can use the EF6 extention from automapper themselves
https://www.nuget.org/packages/AutoMapper.EF6/
then you can just use
.ProjectTo<TDestination>(mapperConfig)
on the list that needs to be mapped
You don't need to map from a list to a list - Automapper knows about lists, you just need to tell it about the individual items:
Mapper.Initialize(x =>
{
x.CreateMap<Pais, PaisDto>().ReverseMap();
});
Then it can map a list by itself:
IEnumerable<Pais> paises = _paisRepository.GetPaisAll();
List<PaisDto> dto = Mapper.Map<List<PaisDto>>(paises);
return dto;
You also don't need to call .ToList on lists, that just makes another copy.
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.