Search code examples
c#automapperautomapper-4

Automapper doesn't work as it should


I am using AutoMapper 4.2.1.0 and I have defined my maps as follow.

 var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Order, OrderDTO>();
            cfg.CreateMap<Order_Detail, Order_DetailDTO>();
        });
MapperConfig = config;

Then I use MapperConfig in my code to do this :

var builder = MapperConfig.ExpressionBuilder;
return ((IQueryable<TEntity>) property.GetValue(_db, null)).ProjectTo<TDto>(builder);

but when TEntity is Order and TDto is OrderDto i am getting an exception that says :

Missing map from Order to OrderDTO. Create using Mapper.CreateMap

What did I do wrong ?


Solution

  • OK. I have got it: Instead of :

    return ((IQueryable<TEntity>) property.GetValue(_db, null)).ProjectTo<TDto>(builder);
    

    I should write :

    return ((IQueryable<TEntity>) property.GetValue(_db, null)).ProjectTo<TDto>(MapperConfig);
    

    Passing the config object itself into ProjectTo.