I'm using Entity Framework 6.2.0 and Automapper 6.2.2. I need to map the entity Cart
to CartDto
. CartDto
has a property Total
which needs to be mapped to the result of Cart.GetTotal()
. I'd like to use .ProjectTo
to simplify the query, but if I do that I receive the error:
LINQ to Entities does not recognize the method GetTotal()
because the projection uses IQueryable
and the method has no translation in SQL. Is there any way around this issue?
var automapperConfiguration = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Cart, CartDto>()
.ForMember(x => x.Total, o => o.MapFrom(x => x.GetTotal()))
.ForAllOtherMembers(x => x.Ignore());
});
var cartDto = dbContext.Carts
.ProjectTo<CartDto>(automapperConiguration)
.FirstOrDefault();