我有以下模型:
public class Order
{
public long OrderId { get; set; }
public ICollection<Order_OrderItem> Order_OrderItems { get; set; }
}
public class Order_OrderItem
{
public long OrderId { get; set; }
public virtual Order Order { get; set; }
public int OrderItemId { get; set; }
public virtual OrderItem OrderItem { get; set; }
}
我在下面创建了DTO:
public class OrderDto
{
public long OrderId { get; set; }
public IEnumerable<OrderItemDto> OrderItems { get; set; }
}
如何将Order
映射到OrderDto
?
我在下面遇到错误:
configuration.CreateMap<Order, OrderDto>()
.ForMember(desc => desc.OrderItems,
opt => opt.MapFrom(src => src.Order_OrderItems));
我检查了其他相关问题,建议与map属性相关,但我的OrderItems
是一个集合属性。我怎么能映射它?
任何帮助都将不胜感激。
更改以下配置:
configuration.CreateMap<Order_OrderItem, OrderItemDto>();
configuration.CreateMap<Order, OrderDto>()
.ForMember(desc => desc.OrderItems,
opt => opt.MapFrom(src => src.Order_OrderItems));
检查结果时,OrderItems
中的OrderDto
将为null
:
var result = ObjectMapper.Map<OrderDto>(order);
答案 0 :(得分:1)
从OrderItem
映射到OrderItemDto
:
configuration.CreateMap<OrderItem, OrderItemDto>();
从Order
映射到OrderDto
:
configuration.CreateMap<Order, OrderDto>()
.ForMember(dest => dest.OrderItems,
opt => opt.MapFrom(src => src.Order_OrderItems.Select(o => o.OrderItem)));