Automapper 异常:“缺少类型映射配置或不受支持的映射." [英] Automapper exception: "Missing type map configuration or unsupported mapping."
问题描述
我正在尝试在 ASP.NET MVC 5 应用程序中使用 Ninject,该应用程序使用 AutoMapper 将模型映射到视图模型,反之亦然.不幸的是,我收到一条错误消息,指出缺少类型映射配置.
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:
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.
如果我将映射创建直接添加到控制器方法中,它会起作用:
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屋!