Automapper例外:“缺少类型映射配置或不支持的映射”。 [英] Automapper exception: "Missing type map configuration or unsupported mapping."

查看:5905
本文介绍了Automapper例外:“缺少类型映射配置或不支持的映射”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在使用AutoMapper将模型映射到视图模型的ASP.NET MVC 5应用程序中使用Ninject,反之亦然。
不幸的是,我收到一条错误消息,指出类型映射配置丢失。

I am trying to use Ninject in an ASP.NET MVC 5 application that uses AutoMapper for mapping the Model to the View Model and vice versa. Unfortunately I get an error message that states that the type map configuration is missing.

我创建了一个Ninject依赖关系解析器:

I created a Ninject dependency resolver:

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: p>

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();
        }
    }
}

这将调用AutoMapper配置:

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 run 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

我想念某件事
在使用Ninject依赖解析器之前,它工作正常。
现在似乎找不到映射。

Do I miss something. It worked fine before I used the Ninject dependency resolver. Now it does not seem to find the mappings.

如果我将Mapping Creation直接添加到控制器方法中:

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);
}

映射本身和模型和视图模型似乎不是问题。我想这个程序在某种程度上找不到映射。

The mapping itself and the models and view models do not seem to be the problem. I guess that the programm somehow does not find the mappings.

即使我在控制器方法中调用Auto Mapper Web Configuration,它也可以工作:

Even if I call the 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);
}


推荐答案

您需要添加相反的映射也。您可以通过以下两种方法之一:

You need to add the reverse mapping also. You can do it one of two ways:

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

或一次如此:

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

如果您问我,后者是更好的。

The latter is preferable if you ask me.

这篇关于Automapper例外:“缺少类型映射配置或不支持的映射”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆