Rob Bowman Rob Bowman - 2 years ago 440
C# Question

Auto Mapper v4.2.1 - Missing type map configuration or unsupported mapping

I have an mvc application developed with VS2015.

In the Application_Start method of Global.asax.cs a method containing the following is called:

Mapper.Initialize(cfg => cfg.CreateMap<Domain.ESB.Fault, Models.FaultVmRec>());
Mapper.Initialize(cfg => cfg.CreateMap<Models.FaultVmRec, Domain.ESB.Fault>());

I've checked that this does get executed by adding a breakpoint.

In my controller, I have the following:

faultVmRec = Mapper.Map<Fault, FaultVmRec>(item);

At this line, I'm getting the exception:

{"Missing type map configuration or unsupported mapping.\r\n\r\nMapping types:\r\nFault -> FaultVmRec\r\nLGSS.Unit4.BamPortal.Domain.ESB.Fault -> LGSS.Unit4.BamPortal.Website.Models.FaultVmRec\r\n\r\nDestination path:\r\nFaultVmRec\r\n\r\nSource value:\r\nSystem.Data.Entity.DynamicProxies.Fault_805BF7128A26886D33A8989DFA212C8E378EDADE588B0922B23ECFE7F697D907"}

However, if I paste in to explicitly initialize the mapping before executing the map as follows:

Mapper.Initialize(cfg => cfg.CreateMap<Domain.ESB.Fault, Models.FaultVmRec>());
faultVmRec = Mapper.Map<Fault, FaultVmRec>(item);

Then it works fine!

Anyone see where I'm going wrong please?

Answer Source

Mapper.Initialize(cfg => cfg.CreateMap<Domain.ESB.Fault, Models.FaultVmRec>)); Mapper.Initialize(cfg => cfg.CreateMap<Models.FaultVmRec, Domain.ESB.Fault>());

This code snippet overrides each other. You should configure as:

Mapper.Initialize(cfg => {
  cfg.CreateMap<Domain.ESB.Fault, Models.FaultVmRec>();
  cfg.CreateMap<Models.FaultVmRec, Domain.ESB.Fault>();
  cfg... //so on

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download