Automapper exception: "Missing type configuration or unsupported mapping."

I am trying to use Ninject in an ASP.NET MVC 5 application that uses AutoMapper to map a model to a view model and vice versa. Unfortunately, I get an error message indicating that the type map configuration is missing.

I created a Ninject dependency converter:

namespace MyNamespace.Infrastructure { public class NinjectDependencyResolver: IDependencyResolver { private IKernel kernel; public NinjectDependencyResolver(IKernel kernelParam) { kernel = kernelParam; AddBindings(); } public object GetService(Type serviceType) { return kernel.TryGet(serviceType); } public IEnumerable<object> GetServices(Type serviceType) { return kernel.GetAll(serviceType); } private void AddBindings() { kernel.Bind<IMyBLL>().To<MyBLL>(); } } } 

I use this to create a controller:

 namespace MyNamespace.Controllers { [Authorize] public class HomeController : Controller { private IMyBLL _myBLL; public HomeController(IMyBLL myBLLParam) { _myBLL = myBLLParam; } public PartialViewResult AddRecord() { return PartialView(new AddRecordViewModel()); } [HttpPost] public void AddRecord(AddRecordViewModel recordViewModel) { var record = Mapper.Map<Record>(recordViewModel); _myBLL.AddRecord(record, User.Identity.Name); } } } 

Global.asax:

 namespace MyNamespace.WebApplication { public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { ApplicationUserManager.StartupAsync(); AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); AutoMapperWebConfiguration.Configure(); } } } 

This calls the AutoMapper configuration:

 namespace MyNamespace.WebApplication.Infrastructure { public static class AutoMapperWebConfiguration { public static void Configure() { Mapper.Initialize(cfg => cfg.AddProfile(new RecordProfile())); } } public class RecordProfile : Profile { protected override void Configure() { Mapper.CreateMap<AddRecordViewModel, Record>().ReverseMap(); } } } 

When I ran this, I get the following error message:

 Missing type map configuration or unsupported mapping. Mapping types: AddRecordViewModel -> Record MyNamespace.WebApplication.ViewModels.Home.AddRecordViewModel -> MyNamespace.Model.Record Destination path: Record Source value: MyNamespace.WebApplication.ViewModels.Home.AddRecordViewModel 

I will miss something. It worked fine before I used the Ninject dependency converter. Now he does not find a match.


Edit:

If I add the Mapping creation directly to the controller method, it works:

 [HttpPost] public void AddRecord(AddRecordViewModel recordViewModel) { Mapper.CreateMap<AddRecordViewModel, Record>().ReverseMap(); var record = Mapper.Map<Record>(recordViewModel); _myBLL.AddRecord(record, User.Identity.Name); } 

Display itself and models and presentation models are not a problem. I assume that the program somehow does not find a match.

Even if I call Auto Mapper Web Configuration in the controller method, it works:

 public void AddRecord(AddRecordViewModel recordViewModel) { Infrastructure.AutoMapperWebConfiguration.Configure(); var record = Mapper.Map<Record>(recordViewModel); _myBLL.AddRecord(record, User.Identity.Name); } 
+7
c # dependency-injection asp.net-mvc-5 ninject automapper
source share
5 answers

You also need to add a reverse mapping. You can do this in one of two ways:

 Mapper.CreateMap<AddRecordViewModel, Record>(); Mapper.CreateMap<Record, AddRecordViewModel>(); 

or in one motion:

 Mapper.CreateMap<AddRecordViewModel, Record>().ReverseMap(); 

The latter is preferable if you ask me.

+12
source share

Do not call Mapper.CreateMap in your profile. Call base.CreateMap and you set:

 public class RecordProfile : Profile { protected override void Configure() { base.CreateMap<AddRecordViewModel, Record>().ReverseMap(); } } 
+4
source share

I had a similar problem, I forgot to register at Global.asax

 public class WebApiApplication : System.Web.HttpApplication { protected void Application_Start() { AutoMapperConfig.RegisterMappings(); } } 
0
source share

In my case, the problem was that some Automapper mappings were registered with IoC by default.

 builder.Register(_ => AutomapperConfiguration.Configure()).As<IMapper>().SingleInstance(); 

A mapping that was unsuccessful was registered elsewhere, service configuration

 static void Main(string[] args) { var container = ConfigureDependencies(); AutoMapping.Configure(); 

The project was that this error was a test project in which service configuration was not completed. When the debugged one had the illusion that failed mappings were logged, as could be seen from the IoC mappings.

Solution, make sure all mappings have been registered in the test solution.

0
source share

Mapper.Initialize() should be used strictly once for each solution.

If you call Initialize() somewhere later, it will override all your previous mappings. Examine your code carefully, suppose you find a call to this method elsewhere.

PS: Previously, this was not the original Automapper behavior, as I could see in pieces of code created 3 or more years ago on GitHub.

0
source share

All Articles