I followed the official documentation, stating the shift from static API as regards Automapper 9.0 but still I am unable to make it work inside my c# dotnet core App. I get the following exception:
"AutoMapperMappingException: missing type map configuration or unsupported mapping" in my controller class."
Can you show me please the configuration steps as regards Automapper 9.0?
Here the steps I performed:
Startup.cs
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new MyMapperProfile());
});
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
MyMapperProfile class
CreateMap<MasterData.Models.SupplierMaterial, SupplierMaterial>().
ForMember(dto => dto.TechnicalDrawings, conf => conf.MapFrom(ol =>
ol.TechnicalDrawings.Select(v => v.Id)));
CreateMap<MasterData.Models.WarehouseUbication, WarehouseUbication>().
ForMember(dto => dto.MaterialCode, conf => conf.MapFrom(ol => ol.Material.Code));
CreateMap<MasterData.Models.TechnicalDrawing, TechnicalDrawing>().
ForMember(dto => dto.MaterialCode, conf => conf.MapFrom(ol => ol.Materials.Select(v => v.Code)))
.ForMember(dto => dto.SupplierMaterialCode, conf => conf.MapFrom(ol => ol.SupplierMaterials.Select(v => v.Code)));
CreateMap<MasterData.Models.MaterialDocument, MaterialDocument>().
ForMember(dto => dto.Materials, conf => conf.MapFrom(ol => ol.Materials.Select(v => v.Code)));
CreateMap<MasterData.Models.Material, Material>().
ForMember(dto => dto.WarehouseUbications, conf => conf.MapFrom(ol => ol.WarehouseUbications.Select(v => v.Code)))
.ForMember(dto => dto.SupplierMaterials, conf => conf.MapFrom(ol => ol.SupplierMaterials.Select(v => v.Code)))
.ForMember(dto => dto.TechnicalDrawingID, conf => conf.MapFrom(ol => ol.TechnicalDrawing.Id))
.ForMember(dto => dto.MaterialDocuments, conf => conf.MapFrom(ol => ol.MaterialDocuments.Select(v => v.Materials)));
//var mapper = config.CreateMapper();
}
}
}
Controller class
public MaterialController(IMasterDataService masterService, IMapper mapper)
{
this.masterDataService = masterService;
_mapper = mapper;
}
//[route]
public List<Material> getAllMaterials()
{
List<Material> materialsList = new List<Material>();
foreach (MasterData.Models.Material m in masterDataService.GetMaterials())
{
_mapper.Map<IEnumerable<Contracts.Material>>(m);
}
return materialsList;
}
The error is in your controller code. You should either map directly from one collection type to another, using the built in collection mapping support, or map each element indiviually, building the target collection yourself. Currently, you're just discarding the mapped objects and returning an empty list.
or: