The problem is that AutoMapper does not know how you want to convert it. All Food items are derived from IFood , so displaying its execution is the simplest (and is correct). You can force a matching mapping by creating a TypeConverter - something like this might work:
public class FoodConverter : TypeConverter<IFood, IFood> { protected override IFood ConvertCore(IFood source) { if (source is PickleDto) return Mapper.Map<Pickle>(source); if (source is BananaDto) return Mapper.Map<Banana>(source); return null; } }
This can be configured in your mapping as follows:
Mapper.CreateMap<IFood, IFood>().ConvertUsing<FoodConverter>();
Personally, I would have thought a little that he has DTO foods:
interface IFoodDto { string Name { get; set; } }
This will slightly change your intention before AutoMapper .
Finally, remember to call AssertConfigurationIsValid in your mapping.
source share