Automapper 8的Ignore属性在子后代上不起作用。
创始人
2024-09-23 09:00:58
0

在AutoMapper 8中,Ignore属性在子后代上不会自动起作用。这是因为AutoMapper默认仅在直接映射目标类型的属性时应用Ignore属性。

要解决这个问题,可以使用AutoMapper的Flattening配置选项。Flattening配置选项允许你在映射期间扁平化对象图,以便更灵活地忽略属性。

下面是一个示例代码,展示了如何使用Flattening配置选项来解决这个问题:

using AutoMapper;

public class SourceClass
{
    public int Id { get; set; }
    public string Name { get; set; }
    public SubSourceClass SubSource { get; set; }
}

public class SubSourceClass
{
    public string SubName { get; set; }
    public ChildSourceClass ChildSource { get; set; }
}

public class ChildSourceClass
{
    public string ChildName { get; set; }
}

public class DestinationClass
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string SubName { get; set; }
}

public class AutoMapperProfile : Profile
{
    public AutoMapperProfile()
    {
        CreateMap()
            .ForMember(dest => dest.SubName, opt => opt.MapFrom(src => src.SubSource.SubName))
            .ForMember(dest => dest.Name, opt => opt.Ignore())
            .ForMember(dest => dest.Id, opt => opt.Ignore())
            .ForPath(dest => dest.SubName, opt => opt.Ignore());
    }
}

public class Program
{
    public static void Main()
    {
        var configuration = new MapperConfiguration(cfg =>
        {
            cfg.AddProfile(new AutoMapperProfile());
        });
        var mapper = configuration.CreateMapper();

        var source = new SourceClass
        {
            Id = 1,
            Name = "SourceName",
            SubSource = new SubSourceClass
            {
                SubName = "SubSourceName",
                ChildSource = new ChildSourceClass
                {
                    ChildName = "ChildSourceName"
                }
            }
        };

        var destination = mapper.Map(source);

        Console.WriteLine(destination.SubName);
        // Output: SubSourceName
    }
}

在上面的示例中,我们使用了ForMember方法来映射SourceClass中的SubSource.SubName属性到DestinationClass的SubName属性,并使用Ignore方法来忽略DestinationClass的Name和Id属性。最后,我们使用ForPath方法来忽略DestinationClass的SubName属性。

当使用Flattening配置选项时,需要注意:

  • 必须使用ForMember方法来映射扁平化后的属性。
  • 必须在映射配置中使用Ignore方法来忽略原始属性。
  • 必须使用ForPath方法来映射扁平化后的属性,然后使用Ignore方法来忽略它。

通过使用Flattening配置选项,我们可以在子后代上忽略属性,并且可以灵活地控制映射行为。

相关内容

热门资讯

透视私人局(WEPoke)德扑... 透视私人局(WEPoke)德扑软件决策(透视)详细辅助爆料教程;最新版2024是一款经典耐玩的益智游...
透视规律!wepoker透视脚... 透视规律!wepoker透视脚本免费(透视)起初是真的有挂(详细辅助插件教程);原来确实真的有挂(需...
透视游戏!aapoker辅助工... 透视游戏!aapoker辅助工具存在吗(透视)详细辅助我来教教你(果然存在有挂)1、任何aapoke...
透视ai(wepOkE)微扑克... 透视ai(wepOkE)微扑克辅助钻石(透视)详细辅助细节方法准备好在微扑克辅助钻石ia的高塔上攀登...
透视线上!wepoker游戏安... 透视线上!wepoker游戏安装教程(透视)一向是真的有挂(详细辅助技巧教程)是由北京得wepoke...
透视代打!智星菠萝辅助怎么买(... 透视代打!智星菠萝辅助怎么买(透视)详细辅助系统教程(切实真的是有挂)1、智星菠萝辅助怎么买ai辅助...
透视免费(wepOke)wep... 透视免费(wepOke)wepoke透明挂辅助软件是真的吗(透视)详细辅助2025新版;科技安装教程...
透视挂透视!wepokerpl... 透视挂透视!wepokerplus透视脚本免费(透视)从来是有挂(详细辅助介绍教程);实战中需综合运...
透视新版!aapoker辅助插... 透视新版!aapoker辅助插件工具下载(透视)详细辅助曝光教程(切实是有挂);1、aapoker辅...
透视讲解(WepoKe)wep... 透视讲解(WepoKe)wepoker真的有挂吗(透视)详细辅助可靠教程;实战中需综合运用上述技巧,...