Rob Bowman
Rob Bowman

Reputation: 8741

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

I have an asp.net 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?

Upvotes: 2

Views: 4040

Answers (2)

mxmissile
mxmissile

Reputation: 11681

Calls to Initialize instantiate a new configuration. Combine them like this:

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

Also you should validate your configuration after you Initialize to catch potential runtime errors documented here:

Mapper.Configuration.AssertConfigurationIsValid();

Upvotes: 3

Oğuzhan Soykan
Oğuzhan Soykan

Reputation: 2710

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.
  cfg... //so on

});

Upvotes: 5

Related Questions