Jack Tyler
Jack Tyler

Reputation: 529

AutoMapper exception When converting one DTO to another object

I am trying to run a test to check that my mapping is correct however every time I run the debugger I get an AutoMapperMappingException.

My code:

public BB.LMS.Models.CaseExport ConvertStarsCaseExportToCaseExport(BB.LMS.Services.Core.Models.Stars.caseexport caseExport)
{
    Mapper.CreateMap<BB.LMS.Services.Core.Models.Stars.caseexport, CaseExport>();

    var ConvertedCase = Mapper.Map<BB.LMS.Services.Core.Models.Stars.caseexport, BB.LMS.Models.CaseExport>(caseExport);
    return ConvertedCase;

}

and

[TestMethod()]
public void ConvertToCaseTest()
{
    DTOService service = new DTOService();

    caseexport export = xmlService.DeserializeStarsExport(testStarsFile);
    CaseExport convertedCase = service.ConvertStarsCaseExportToCaseExport(export);

Exception:

{ "Missing type map configuration or unsupported mapping.\r\n\r\nMapping types:\r\ncase -> Case\r\nBB.LMS.Services.Core.Models.Stars.case -> BB.LMS.Models.Case\r\n\r\nDestination path:\r\nCaseExport.solicitor.solicitor.case.case\r\n\r\nSource value:\r\nBB.LMS.Services.Core.Models.Stars.case" }

FIXED: As Sergey L correctly pointed out, I hadn't mapped case -> Case once that had been mapped my code worked a treat!

Upvotes: 0

Views: 226

Answers (1)

Yvain
Yvain

Reputation: 952

The error says automapper needs configuration in order to be able to map.

Here is one way to do it :

    public BB.LMS.Models.CaseExport ConvertStarsCaseExportToCaseExport(BB.LMS.Services.Core.Models.Stars.caseexport caseExport)
    {
        var config = new MapperConfiguration(cfg =>
            {
                 cfg.CreateMap<BB.LMS.Services.Core.Models.Stars.caseexport, CaseExport>();
            });
        var mapper = config.CreateMapper();
        var ConvertedCase = mapper.Map<BB.LMS.Services.Core.Models.Stars.caseexport, BB.LMS.Models.CaseExport>(caseExport);
        return ConvertedCase;
    }

Upvotes: 2

Related Questions