我在类中嵌套了对象,当我通过自动映射器应用映射时,我只是在所有字段上得到 null。
我的源类obj:
public class TechDataVariant
{
public Measurement Acceleration { get; set; }
}
public class Measurement
{
public string Label { get; set; }
public string Value { get; set; }
}
我的目的地班级:
public class TechData
{
public Measurement Acceleration { get; set; }
}
public class Measurement
{
public string Label { get; set; }
public string Value { get; set; }
}
我的地图绘制者:
CreateMap<TechDataVariant, TechData>()
.IgnoreAllUnmapped()
.ForPath(dest => dest.Acceleration.Label, opt => opt.MapFrom(source => source.Acceleration.Label))
.ForPath(dest => dest.Acceleration.Value, opt => opt.MapFrom(source => source.Acceleration.Value));
我的地图:
var TechDataVariant = new TechDataVariant
{
Acceleration = new BusinessLogic.Models.Measurement
{
Label = variant.LabelForPerformanceRange,
Value = variant.Performance
}
};
var techDataVariantResult = mapper.Map<TechData>(TechDataVariant);
我的结果:
输出值(techDataVariantResult .Acceleration):
我的忽略扩展:
public static class MappingExpressionExtensions
{
public static IMappingExpression<TSource, TDest> IgnoreAllUnmapped<TSource, TDest>(this IMappingExpression<TSource, TDest> expression)
{
expression.ForAllMembers(opt => opt.Ignore());
return expression;
}
}
我错过了什么?
谢谢你@LucianBargaoanu。
出于某种原因,我无法完全解释我的扩展方法,其本意是让生活变得轻松,但却让生活变得困难。
我删除了它并忽略了这样的道具:
.ForMember(dest => dest.PropToIgnore, opt => opt.Ignore())