[英]Automapper error after updating a record with POCO classes, Unity and EF6
I've created an application that uses automapper, everything seems to be configured and working correctly in browse mode, however after I update a record I then get the following mapping error: 我创建了一个使用自动映射器的应用程序,所有内容似乎都已配置并且可以在浏览模式下正常工作,但是在更新记录后,我得到了以下映射错误:
AutoMapper.AutoMapperMappingException was unhandled by user code
HResult=-2146233088
Message=Missing type map configuration or unsupported mapping.
Mapping types:
Organisation -> ViewModelOrganisation
I've registered auttomapper in application start: 我已经在应用程序启动中注册了auttomapper:
protected void Application_Start()
{
App_Start.AutoMapperConfig.Initialize();
}
Then done the mapping in Automapperconfig: 然后在Automapperconfig中完成映射:
public class AutoMapperConfig
{
public static void Initialize()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Organisation, ViewModelOrganisation>().ReverseMap();
cfg.CreateMap<Article, ViewModelArticle>().ReverseMap();
cfg.CreateMap<Organisation, ViewModelAdminOrg>().ReverseMap();
cfg.CreateMap<Branch, ViewModelBranch>().ReverseMap();
});
}
}
This hits OK when the application starts and I can browse the site. 当应用程序启动时,单击确定,我可以浏览该站点。 The problem occurs when I save a record (update). 当我保存记录(更新)时,会出现问题。 The information saves, however when I go back to another page to browse the site I get mapping errors. 该信息会保存,但是当我返回另一个页面浏览该站点时,会出现映射错误。
Update: 更新:
Im mapping in the controller like so: 我在控制器中的映射如下:
public ActionResult Detail(int id)
{
Organisation org = new Organisation();
ViewModelOrganisation vm = new ViewModelOrganisation();
org = _orgService.getOrganisationByOrgID(id);
vm = Mapper.Map(org, vm);
return View(vm);
}
The error occurs on the line: vm = Mapper.Map(org, vm). 错误发生在以下行:vm = Mapper.Map(org,vm)。 It also occurs on other pages that use the mapper. 它也发生在使用映射器的其他页面上。 But only after I have updated a record in my admin panel. 但只有在我更新了管理控制台中的记录后,才可以。
As your full exception message says, the mapper doesn't have a mapping from Organisation
to ViewModelOrganisation
. 如您的完整异常消息所述,映射器没有从Organisation
到ViewModelOrganisation
的映射。 I'm not sure, but is next to the reverse mapping not also a normal mapping needed? 我不确定,但是在反向映射旁边还不需要法线映射吗? So try adding cfg.CreateMap<Organisation, ViewModelOrganisation>()
. 因此,请尝试添加cfg.CreateMap<Organisation, ViewModelOrganisation>()
。
Also you can simplify your code to this: 您也可以将代码简化为:
public ActionResult Detail(int id)
{
var org = _orgService.getOrganisationByOrgID(id);
var vm = Mapper.Map<ViewModelOrganisation>(org);
return View(vm);
}
Prior to initializing the mapper in global.asa i was doing this in the controller itself. 在global.asa中初始化映射器之前,我是在控制器本身中执行此操作的。 I had failed to remove the line(below), from the controller, where the article record was being edited: 我未能从正在编辑文章记录的控制器中删除行(下方):
Mapper.Initialize(cfg => cfg.CreateMap<Article, ViewModelArticle>());
This must have invalidated the mapping created on start up and hence my error when I went to browse the rest of the site. 这一定会使启动时创建的映射无效,因此,当我去浏览站点的其余部分时出现了我的错误。
Lesson learned...Ensure to only initialise the mapper once! 经验教训...确保仅初始化一次映射器!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.