I am currently using a .NET Web API, to get a list of Counties based on Countries. While attempting to GET the Counties, I get the exception:
"Automapper: Missing type map configuration or unsupported mapping"
My Domain Class is :
public class County
{
public string Id { get; set; }
public string Name { get; set; }
public string CountryId { get; set; }
public virtual Country Country { get; set; }
public virtual IList<City> City { get; set; }
}
My DTO is
public class CountyResponse
{
public string Id { get; set; }
public string Name { get; set; }
public string CountryId { get; set; }
}
Mapping profile :
public class CountyProfile : Profile
{
public CountyProfile()
{
CreateMap<CountyResponse, County>().ReverseMap();
}
}
My Service Class GetAlLMethod:
public async Task<Result<List<CountyResponse>>> GetAllAsync()
{
var counties = _context.Counties.ToListAsync();
var mappedCounties = _mapper.Map<List<CountyResponse>>(counties);
return await Result<List<CountyResponse>>.SuccessAsync(mappedCounties);
}
Builds Ok, and I also have the Country domain mapped exactly the same (1:1). Yet, one works, while this one, when trying to access the Get Endpoint, i get this error. Any idea what is wrong here ? (the services are registered - I am using the following to inject them:
public static void AddInfrastructureMappings(this IServiceCollection services)
{
services.AddAutoMapper(Assembly.GetExecutingAssembly());
}
You are mapping a task (the result of your ToListAsync) to a list of your DTO which fails. You need to await your query so you get the result of the task, then the mapping will work properly
var counties = await _context.Counties.ToListAsync();
var mappedCounties = _mapper.Map<List<CountyResponse>>(counties);