防止Automapper创建具有空值的对象属性 [英] Prevent Automapper creating object property with null values

查看:43
本文介绍了防止Automapper创建具有空值的对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Automapper与EF Core配合使用.EF实体:

I'm using Automapper with EF Core. EF entities:

public class Team
{
    public Guid? Id { get; set; }

    public string Name { get; set; }

    public Guid? OrganizationId { get; set; }

    public Organization Organization { get; set; }
}

public class Organization
{
    public Guid Id { get; set; }

    public string Name { get; set; }

    public ICollection<Team> Teams { get; set; }
}

以及相应的DTO:

public class TeamDto
{
    public Guid? Id { get; set; }

    public string Name { get; set; }

    public OrganizationDto Organization { get; set; }
}

public class OrganizationDto
{
    public Guid? Id { get; set; }

    public string Name { get; set; }
}

类型映射:

cfg.CreateMap<Organization, OrganizationDto>();

cfg.CreateMap<TeamDto, Team>()
    .ForMember(teamEntity => teamEntity.Organization, opt => opt.Ignore())
    .ForMember(teamEntity => teamEntity.OrganizationId, opt => opt.MapFrom(
        orgDto => orgDto == null ? null : orgDto.Id))
    .ReverseMap();

当我尝试从Team实体映射时->dto,当Team实体上的 Organization 为null时,TeamDto具有不为null值的null null Organization 属性.例如

When I try to map from Team entity -> dto, when Organization is null on Team entity, TeamDto has non null Organization property with null values. e.g.

var team = new Team
{
    Name = "New Team"
};

// team.Organization is null as well as team.OrganizationId
var teamDto = s_mapper.Map<TeamDto>(team);   

这是不可取的,因为我希望TeamDto的 Organization 属性为null.我尝试通过 ForMember 配置此映射,但未生效:

This is not desirable, as I want TeamDto's Organization property to be null. I tried to configure this mapping via ForMember but it didn't take effect:

cfg.CreateMap<TeamDto, Team>()
    .ForMember(teamEntity => teamEntity.Organization, opt => opt.Ignore())
    .ForMember(teamEntity => teamEntity.OrganizationId, opt => opt.MapFrom(
        orgDto => orgDto == null ? null : orgDto.Id))
    .ReverseMap()
    .ForMember(teamDto => teamDto.Organization, opt => opt.MapFrom(
        teamEntity => teamEntity.Organization == null ? null : new OrganizationDto
    {
        Id = teamEntity.Organization.Id,
        Name = teamEntity.Organization.Name
    }));

如何将其配置为在source也是null时将 Organization 属性设置为null.

How can I configure this to have Organization property be set to null when source is also null.

我能做到这一点的一种方法是使用 AfterMap ,但是我正在寻找一种可以应用于所有此类情况的解决方案,而无需为每种情况都指定 AfterMap

One way I can achieve this is by using AfterMap, but I'm looking for a solution that can be applied to all such cases without specifying AfterMap for each case.

AutoMapper 9.0.0版

AutoMapper v. 9.0.0

推荐答案

有时 ReverseMap 会遇到麻烦,在这里似乎就是这种情况.您总是可以避免它,而只需显式地创建反向映射即可.

Sometimes ReverseMap just gets in the way, as it seems to be the case here. You can always avoid it and simply create the reverse map explicitly.

这篇关于防止Automapper创建具有空值的对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆