我有类似的东西
public class AViewModel
{
public decimal number { get; set; }
public List<BViewModel> BVM { get; set; }
}
public class BViewModel
{
public string someString{ get; set; }
}
public class SomeObject
{
public decimal number { get; set; }
public List<OtherObjects> BVM { get; set; }
}
public class OtherObjects {
public string someString{ get; set; }
}
Mapper.CreateMap<SomeObject,AViewModel>();
当我有这个时,我得到了
如何帮助它弄清楚如何正确映射?
答案 0 :(得分:5)
我相信Automapper需要知道如何将OtherObject转换为BViewModel。尝试为此添加映射。
答案 1 :(得分:0)
您需要通过指定custom type converter
在OtherObject和BViewModel之间指定一个typeconverter这是转换器的样子:
public class OtherToBViewTypeConverter : ITypeConverter<OtherObjects, BViewModel>
{
public BViewModel Convert(ResolutionContext context)
{
if (context.IsSourceValueNull) return null;
var otherObjects = context.SourceValue as OtherObjects;
return new BViewModel { someString = otherObjects.someString; }
}
}
然后地图就像这样调用:
Mapper.CreateMap<SomeObject,AViewModel>().ConvertUsing<OtherToBViewTypeConverter>();