1

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 ?

Beatles1692
  • 5,214
  • 34
  • 65

2 Answers2

2

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.

Beatles1692
  • 5,214
  • 34
  • 65
1

You need to create a mapper using the MapperConfiguration object.

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

// Make sure mappings are properly configured (you can try-catch this).
config.AssertConfigurationIsValid();

// Create a mapper to use for auto mapping.
var mapper = config.CreateMapper();

var orderObject = new Order { /* stuff */ };
var orderDto = mapper.Map<OrderDTO>(orderObject);
  • 1
    I want to use `ProjectTo` so I have to use ExpressionBuilder like it is mentioned in this article : https://lostechies.com/jimmybogard/2016/01/21/removing-the-static-api-from-automapper/ – Beatles1692 Apr 06 '16 at 12:27