0

I am trying to use automapper in my new minimal api's project that i connected with cosmos db. When i try to get all of my objects i get the error: "Missing type map configuration or unsupported mapping" what's wrong ?

My CarRequests class:

public static class CarRequests
{
    public static WebApplication CarEndpoints(this WebApplication app)
    {
        app.MapGet("/cars", CarRequests.GetAll);
        return app;
    }

    public static IResult GetAll(ICarService service, IMapper mapper)
    {
        var cars = service.GetAll();
        var result = mapper.Map<List<CarDto>>(cars);
        return Results.Ok(result);
    }
}

Car class:

public class Car
{
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }
    [JsonProperty(PropertyName = "producent")]
    public Producent Producent { get; set; }
    [JsonProperty(PropertyName = "age")]
    public int Age { get; set; }
    [JsonProperty(PropertyName = "yearCreated")]
    public int YearCreated { get; set; }
    [JsonProperty(PropertyName = "engine")]
    public Engine Engine { get; set; }

    public Car()
    {
        Id= Guid.NewGuid().ToString();
        YearCreated = DateTime.Now.Year - Age;
    }
}

Engine class:

public class Engine
{
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }
    [JsonProperty(PropertyName = "horsePower")]
    public int HorsePower { get; set; }
    [JsonProperty(PropertyName = "description")]
    public string Description { get; set; }

    public Engine()
    {
        Id= Guid.NewGuid().ToString();
    }
}

EngineDto:

public class EngineDto
{
    [JsonProperty(PropertyName = "horsePower")]
    public int HorsePower { get; set; }
    [JsonProperty(PropertyName = "description")]
    public string Description { get; set; }
}

CarDto:

public class CarDto
{
    [JsonProperty(PropertyName = "producent")]
    public Producent Producent { get; set; }
    [JsonProperty(PropertyName = "age")]
    public int Age { get; set; }
    [JsonProperty(PropertyName = "yearCreated")]
    public EngineDto Engine { get; set; }
}

my service:

public async Task<IEnumerable<Car>> GetAll()
    {
        string queryString = "select * from c";

        var feedIterator = _container.GetItemQueryIterator<Car>(new QueryDefinition(queryString));
        List<Car> cars = new List<Car>();

        while (feedIterator.HasMoreResults)
        {
            var response = await feedIterator.ReadNextAsync();
            cars.AddRange(response.ToList());
        }
        return cars;
    }

Mapper profile:

public class AutoMapperProfile:Profile
{
    public AutoMapperProfile()
    {
        CreateMap<Car, CarDto>();
        CreateMap<Engine, EngineDto>();
        CreateMap<CarDto, Car>();
        CreateMap<EngineDto, Engine>();
    }
}

I also register my automapper in Program.cs file:

builder.Services .AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
Ptrk12
  • 15
  • 3

1 Answers1

1

For you Task<IEnumerable<Car>> GetAll()

Add await to get IEnumerable<Car> for mapping

var cars = await service.GetAll();           // return IEnumerable<Car>
var result = mapper.Map<List<CarDto>>(cars); // IEnumerable<Car> -> List<CarDto>
Jerryehye
  • 26
  • 4