C# AutoMapper 映射子属性也定义了映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8944577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:19:48  来源:igfitidea点击:

AutoMapper Map Child Property that also has a map defined

c#automapper

提问by Dismissile

I have the following Domain Object:

我有以下域对象:

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

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

I have the following two objects that I want to map to:

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

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

I set up the following maps:

我设置了以下地图:

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

If I map my object using the following call then the parent.Child property is null.

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

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

I know I can write the following:

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

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

Is there a way I can eliminate that second call and have AutoMapper do this for me?

有没有办法可以消除第二次调用并让 AutoMapper 为我执行此操作?

采纳答案by Justin Niessner

You just need to specify that in the mapping:

您只需要在映射中指定:

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

回答by alltej

In Automapper 5.0 and above and if you are using Profile to create mapper:

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

where destination ItemsDtois a:

其中目的地ItemsDto是:

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

and source Items is a:

和源项目是:

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

Then create a mapper profile for the child item/property:

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

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

}

回答by prostynick

Just map the child using self. Tested with AutoMapper 6.1.1.

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

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