这个错误通常是在使用Automapper时,源组件中的属性的类型可能是非空类型(如int)而目标组件中同名属性则可能是可空类型(如int?)。出现这种情况时,Automapper会尝试使用Coalesce运算,但无法处理非空类型和可空类型的匹配。因此,需要在配置自动映射时使用MapFrom方法显式地将源组件属性转换为可空类型。
示例代码:
public class SourceClass { public int Age { get; set; } }
public class DestinationClass { public int? Age { get; set; } }
Mapper.Initialize(cfg =>
{
cfg.CreateMap