C# automapper 嵌套集合

标签 c# automapper

我有一个像这样的简单模型:

public class Order{
   public int Id { get; set; }
   ... ...
   public IList<OrderLine> OrderLines { get; set; }
}

public class OrderLine{
   public int Id { get; set; }
   public Order ParentOrder { get; set; }
   ... ...
}

我用 Automapper 做的是这样的:

    Mapper.CreateMap<Order, OrderDto>();
    Mapper.CreateMap<OrderLine, OrderLineDto>();
    Mapper.AssertConfigurationIsValid();

它抛出一个异常: “OrderDto 中的属性 OrderLineDtos 未映射,添加自定义映射...” 当我们在 Domain 和 DomainDto 中使用自定义语法时,我如何指定 OrderDto 中的集合 OrderLineDtos 对应于 Order 中的 OrderLines?

谢谢

最佳答案

它的工作方式是这样的:

    Mapper.CreateMap<Order, OrderDto>()
        .ForMember(dest => dest.OrderLineDtos, opt => opt.MapFrom(src => src.OrderLines));
    Mapper.CreateMap<OrderLine, OrderLineDto>()
        .ForMember(dest => dest.ParentOrderDto, opt => opt.MapFrom(src => src.ParentOrder));
    Mapper.AssertConfigurationIsValid();

关于C# automapper 嵌套集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1789925/

相关文章:

Automapper 编译映射 asp.net core

dependency-injection - 在 ASP.NET Core 中将依赖项注入(inject)到 Automapper TypeConverter

c# - AutoMapper - 强类型数据集

javascript - 将数据从网站发送到 C# 表单应用程序

c# - AutoMapper 的 AssertConfigurationIsValid 仅在第一次加载时失败?

c# - 在 Telegram Bot 中制作一个内联键盘按钮,指向一个 URL C#

c# - 使用 AutoMapper 将数据从 SuperClass 复制到 SubClass

c# - 从 Invoke 中关闭表单

c# - 如何获取FTP服务器上文件的最后修改日期

c# - 当对象具有集合属性时投影 IQueryable<object> 时 Automapper 失败