AutoMapper Map子属性,也定义了一个映射

时间:2012-01-20 16:24:40

标签: c# automapper

我有以下域对象:

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属性为null。

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为我做这个?

4 个答案:

答案 0 :(得分:81)

您只需在映射中指定:

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)));

答案 1 :(得分:23)

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

import glob
import tkinter as tkr
import os

path = r'C:\Users\C253271\Desktop\Empower data\\'
allpyfiles =glob.glob(os.path.join(path, "*.py"))

for file in allpyfiles:
    file =('Empdata')

def graph():    
    global v
    file.display_graph(v.get())  

root = tkr.Tk()
v = tkr.StringVar()
tkr.Button(root, text='Close',command=root.destroy).grid(row=2, column=1)
tkr.Button(root, text='Plot Graphs', command = graph).grid(row=2, column=0)
root.mainloop()

答案 2 :(得分:6)

在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));
    }

}

答案 3 :(得分:-4)

我之前遇到过这个问题 我解决它的方式是通过双向映射

像这样

 mapper.CreateMap<ChildInputDto, Child>();
 mapper.CreateMap<Child, ChildInputDto>();

我不知道为什么,但它与我合作