当我尝试进行一些自定义映射并且有时将目标属性映射到 null 时,Automapper 会抛出异常以尝试创建目标属性对象。
我已经上传了一个简化的项目到 github 来演示: https ://github.com/dreing1130/AutoMapper_Investigation
这是从我从 Automapper 6 升级到 8 时开始的。
如果我返回一个更新的对象而不是 null 它工作正常(虽然我的应用程序期望在这些情况下该值为 null)
我还确认每次调用映射时都会命中我的映射中的断点以确保没有编译的执行计划
public class Source
{
public IEnumerable<string> DropDownValues { get; set; }
public string SelectedValue { get; set; }
}
public class Destination
{
public SelectList DropDown { get; set; }
}
CreateMap<Source, Destination>()
.ForMember(d => d.DropDown, o => o.MapFrom((src, d) =>
{
return src.DropDownValues != null
? new SelectList(src.DropDownValues,
src.SelectedValue)
: null;
}));
预期结果:当 Source.DropdownValues 为 null 时,Destination.DropDown 为 null
实际结果:抛出异常
“System.Web.Mvc.SelectList 需要有一个带有 0 个参数或只有可选参数的构造函数。参数名称:类型”
冉冉说
相关分类