C# 自动映射异常:“自动映射”;缺少类型映射配置或映射不受支持。”;
我试图在ASP.NET MVC 5应用程序中使用Ninject,该应用程序使用AutoMapper将模型映射到视图模型,反之亦然。 不幸的是,我收到一条错误消息,指出类型映射配置丢失 我创建了一个Ninject依赖项解析程序:C# 自动映射异常:“自动映射”;缺少类型映射配置或映射不受支持。”;,c#,dependency-injection,asp.net-mvc-5,ninject,automapper,C#,Dependency Injection,Asp.net Mvc 5,Ninject,Automapper,我试图在ASP.NET MVC 5应用程序中使用Ninject,该应用程序使用AutoMapper将模型映射到视图模型,反之亦然。 不幸的是,我收到一条错误消息,指出类型映射配置丢失 我创建了一个Ninject依赖项解析程序: namespace MyNamespace.Infrastructure { public class NinjectDependencyResolver: IDependencyResolver { private IKernel ker
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>();
}
}
}
这将调用AutoMapper配置:
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();
}
}
}
我错过了什么吗。
在我使用Ninject依赖解析程序之前,它工作得很好。
现在它似乎没有找到映射
编辑: 如果我将映射创建直接添加到控制器方法中,它将起作用:
[HttpPost]
public void AddRecord(AddRecordViewModel recordViewModel)
{
Mapper.CreateMap<AddRecordViewModel, Record>().ReverseMap();
var record = Mapper.Map<Record>(recordViewModel);
_myBLL.AddRecord(record, User.Identity.Name);
}
[HttpPost]
公共无效AddRecord(AddRecordViewModel recordViewModel)
{
Mapper.CreateMap().ReverseMap();
var record=Mapper.Map(recordViewModel);
_myBLL.AddRecord(记录、用户、标识、名称);
}
映射本身以及模型和视图模型似乎不是问题所在。我猜程序不知何故没有找到映射
即使我在controller方法中调用Auto Mapper Web配置,它也可以工作:
public void AddRecord(AddRecordViewModel recordViewModel)
{
Infrastructure.AutoMapperWebConfiguration.Configure();
var record = Mapper.Map<Record>(recordViewModel);
_myBLL.AddRecord(record, User.Identity.Name);
}
public void AddRecord(AddRecordViewModel recordViewModel)
{
Infrastructure.AutoMapperWebConfiguration.Configure();
var record=Mapper.Map(recordViewModel);
_myBLL.AddRecord(记录、用户、标识、名称);
}
您还需要添加反向映射。您可以使用以下两种方法之一:
Mapper.CreateMap<AddRecordViewModel, Record>();
Mapper.CreateMap<Record, AddRecordViewModel>();
Mapper.CreateMap();
CreateMap();
或者一次就这样:
Mapper.CreateMap<AddRecordViewModel, Record>().ReverseMap();
Mapper.CreateMap().ReverseMap();
如果你问我,后者更可取。不要在你的个人资料中调用Mapper.CreateMap。调用base.CreateMap,即可设置:
public class RecordProfile : Profile
{
protected override void Configure()
{
base.CreateMap<AddRecordViewModel, Record>().ReverseMap();
}
}
公共类RecordProfile:Profile
{
受保护的覆盖无效配置()
{
base.CreateMap().ReverseMap();
}
}
我遇到了类似的问题,我忘了在Global.asax中注册
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AutoMapperConfig.RegisterMappings();
}
}
在我的例子中,问题是一些默认的自动映射已向IoC注册
builder.Register(_ => AutomapperConfiguration.Configure()).As<IMapper>().SingleInstance();
获取此错误的项目是未执行服务配置的测试项目。调试时,会产生注册失败映射的错觉,正如从IoC映射中可以看到的那样
解决方案,确保所有映射都已在测试解决方案中注册。Mapper.Initialize()
应严格按照每个解决方案使用一次
如果稍后在某个地方调用Initialize()
,它将覆盖以前的所有映射。
仔细检查你的代码,猜猜看,你会在另一个地方找到这个方法的调用
注:这并不是Automapper早期的最初行为,正如我在GitHub上3年多前创建的代码片段中所看到的那样 您定义了AddRecordViewModel->Record,但不是相反。另一种方法需要第二个映射。我现在添加了第二个映射。不幸的是,我仍然收到相同的错误消息。程序似乎找不到映射。您的
记录
是否有任何虚拟财产?我正好有这个问题。我有其他使用Queryable扩展(project to)的映射,但是创建的另一个映射继续说没有映射,即使MapperConfiguration明确为这两种类型创建了映射。都在同一个项目中,在同一台机器上运行。请帮忙!哇,我不知道mapperConfig.CreateMapper()正在创建唯一一个具有映射的实例!我必须使用注射来获得映射器,我不能简单地使用Automapper.mapper.Map…谢谢你的帮助。我不知道你可以添加反向映射。这会让事情变得更干净。不幸的是,这不是问题所在。我仍然收到相同的错误消息。我认为程序没有找到映射。我可以在运行时检查映射是否存在吗?那就奇怪了。你能把你的简历贴在这两个班上吗?这样我就可以看到班上的同学了。我不认为这和班上的同学有什么关系。当我将Mapper.CreateMap方法直接添加到controller方法时,它工作得很好(请参阅本文中的其他信息)。只有当我删除它并希望通过Global.asax使用映射时,它才起不到作用。奇怪。一切似乎都配置正确。将视图模型映射到记录模型时出错。当您的AutoMapper配置留在应用程序\u Start
中时,从记录模型到视图模型的映射是否有效?请您解释一下,如何为列表添加从源到扁平类的映射?或者只调用CreateMap,base是隐式的,因为CreateMap是在概要文件中定义的。实际上,我正在进行测试项目,它并没有得到我在AutoMapperConfig中所做的映射。谢谢,现在开始工作了
public class RecordProfile : Profile
{
protected override void Configure()
{
base.CreateMap<AddRecordViewModel, Record>().ReverseMap();
}
}
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AutoMapperConfig.RegisterMappings();
}
}
builder.Register(_ => AutomapperConfiguration.Configure()).As<IMapper>().SingleInstance();
static void Main(string[] args)
{
var container = ConfigureDependencies();
AutoMapping.Configure();