AutoMapper 地图子属性也定义了地图

问题描述 投票:0回答:4

我有以下域对象:

public class DomainClass
{
    public int Id { get; set; }

    public string A { get; set; }
    public string B { get; set; }
}

我有以下两个要映射到的对象:

public class Parent 
{
    public int Id { get; set; }
    public string A { get; set; }

    public Child Child { get; set; }
}

public class Child 
{
    public int Id { get; set; }
    public string B { get; set; }
}

我设置了以下地图:

 Mapper.CreateMap<DomainClass, Parent>();
 Mapper.CreateMap<DomainClass, Child>();

如果我使用以下调用映射我的对象,则parent.Child属性为空。

var domain = GetDomainObject();
var parent = Mapper.Map<DomainClass, Parent>(domain); // parent.Child is null

我知道我可以写以下内容:

var domain = GetDomainObject();
var parent = Mapper.Map<DomainClass, Parent>(domain);
parent.Child = Mapper.Map<DomainClass, Child>(domain);

有没有办法可以消除第二次调用并让 AutoMapper 为我做这件事?

c# automapper
4个回答
114
投票

您只需在映射中指定即可:

Mapper.CreateMap<DomainClass, Child>();
Mapper.CreateMap<DomainClass, Parent>()
      .ForMember(d => d.Id, opt => opt.MapFrom(s => s.Id))
      .ForMember(d => d.A, opt => opt.MapFrom(s => s.A))
      .ForMember(d => d.Child, 
                 opt => opt.MapFrom(s => Mapper.Map<DomainClass, Child>(s)));

更新:

对于最新的 Automapper,请参阅 @prostynick 的回答 -

只需使用 self 来映射孩子。使用 AutoMapper 6.1.1 进行测试。

    CreateMap<DomainClass, Child>();
    CreateMap<DomainClass, Parent>()
        .ForMember(d => d.Child, opt => opt.MapFrom(s => s));

56
投票

只需使用 self 来映射孩子。使用 AutoMapper 6.1.1 进行测试。

        CreateMap<DomainClass, Child>();
        CreateMap<DomainClass, Parent>()
            .ForMember(d => d.Child, opt => opt.MapFrom(s => s));

19
投票

在 Automapper 5.0 及更高版本中,如果您使用 Profile 创建映射器:

public class OrderMapper : Profile
{
    public OrderMapper()
    {
        CreateMap<Order, OrderDto>(MemberList.None)
            .ForMember(dest => dest.OrderId,
                opts => opts.MapFrom(src => src.OrderId))
            .ForMember(dest => dest.OrderDate,
                opts => opts.MapFrom(src => src.OrderDate))
            .ForMember(dest => dest.OrderedBy,
                opts => opts.MapFrom(src => src.OrderedBy))
            .ForMember(dest => dest.ItemsDto,
                opt => opt.MapFrom(src => src.Items));
    }
}

目的地

ItemsDto
是:

 public List<OrderItemDto> ItemsDto { get; set; }

源项目是:

  public List<OrderItem> Items { get; set; }

然后为子项/属性创建映射器配置文件:

public class OrderItemMapper : Profile
{
    public OrderItemMapper()
    {
        CreateMap<OrderItem, OrderItemDto>(MemberList.None)
            .ForMember(dest => dest.ItemId,
                opts => opts.MapFrom(src => src.ItemId))
            .ForMember(dest => dest.ItemPrice,
                opts => opts.MapFrom(src => src.ItemPrice))
            .ForMember(dest => dest.Name,
                opts => opts.MapFrom(src => src.Name))
            .ForMember(dest => dest.Quantity,
                opts => opts.MapFrom(src => src.Quantity))
            .ForMember(dest => dest.ItemTotal,
                opts => opts.MapFrom(src => src.ItemTotal));
    }

}

2
投票

试试这个方法:

 _Mapper.Map<DomainClass, Child>();
         _Mapper.Map<DomainClass, Parent>()
            .ForMember(d => d.Child, opt => opt.MapFrom(s => s));
© www.soinside.com 2019 - 2024. All rights reserved.