【发布时间】:2018-05-16 11:50:25
【问题描述】:
我正在使用 Automapper 将我的视图模型映射到我的域模型。就我而言,我有两个 ViewModel 需要映射到同一个模型。
我为每个视图模型创建两个配置文件,然后在Mapper.Initialize 中添加它们。然后在我的控制器中,当我尝试将我的 ViewModel 映射到模型以便我可以将它传递给服务时,我收到了这个错误Missing type map configuration or unsupported mapping.
我的代码
public static class AutoMapperWebConfiguration
{
public static void Configure()
{
Mapper.Initialize(cfg => cfg.AddProfile(new CompanyRegisterViewModelProfile()));
Mapper.Initialize(cfg => cfg.AddProfile(new CompanyActivateViewModelProfile()));
}
}
然后在我的控制器中:
CompanyEmployee CompanyEmployee = Mapper.Map<CompanyRegisterViewModel, CompanyEmployee>(model,);
现在,如果我删除了第二个配置文件 CompanyActivateViewModelProfile,我不会收到任何错误,并且映射已正确完成。关于我做错了什么有什么想法吗?
【问题讨论】:
-
作为建议,请在 Configure 中调用 Mapper.AssertConfigurationIsValid - 然后 automapper 在启动时抛出错误,而不是在首次使用时抛出错误。
标签: c# asp.net-mvc-5 automapper