升级至AutoMapper 12.0.1后多层子级继承映射失效问题求助
AutoMapper 12.0.1多层继承映射配置失效问题
相同代码在AutoMapper 12.0.0版本中可正常运行,无任何异常;但升级到12.0.1版本后,执行配置验证时会失败。
问题核心:子对象存在三层继承结构,基类映射中已明确配置忽略Parent属性,但该配置在派生类的映射中并未生效。且此问题仅出现在嵌套对象的子级场景下,无嵌套的多层继承不会抛出异常。
完整代码如下:
#nullable enable using System; using AutoMapper; #region Child public class ChildSourceBase { } public class ChildDestinationBase { public ParentDestination Parent { get; set; } = null!; } public class ChildSourceMiddle : ChildSourceBase { } public class ChildDestinationDerived: ChildDestinationBase { } public class ChildSourceDerived : ChildSourceMiddle { } #endregion #region Parent public class ParentSourceBase { } public class ParentDestination { public ChildDestinationBase Child { get; set; } = null!; } public class ParentSourceDerived : ParentSourceBase { public ChildSourceDerived Child { get; set; } = null!; } #endregion public class Program { public static void Main() { var config = new MapperConfiguration(cfg => { #region Parent cfg.CreateMap<ParentSourceBase, ParentDestination>() .ForMember(dest => dest.Child, opt => opt.Ignore()); cfg.CreateMap<ParentSourceDerived, ParentDestination>() .IncludeBase<ParentSourceBase, ParentDestination>() .ForMember(dest => dest.Child, opt => opt.MapFrom(src => src.Child)) ; #endregion #region Child cfg.CreateMap<ChildSourceBase, ChildDestinationBase>() .ForMember(dest => dest.Parent, opt => opt.Ignore()); // `Parent` property should've been ignored here. cfg.CreateMap<ChildSourceMiddle, ChildDestinationDerived>() .IncludeBase<ChildSourceBase, ChildDestinationBase>(); cfg.CreateMap<ChildSourceDerived, ChildDestinationDerived>() // however, here is complaining that `Parent` property is not mapped. .IncludeBase<ChildSourceMiddle, ChildDestinationDerived>(); #endregion }); config.AssertConfigurationIsValid(); } }
内容的提问来源于stack exchange,提问作者Twisted Whisper
相关产品推荐
相关产品推荐

