繁体   English   中英

AutoMapper 从其父属性映射集合属性值

[英]AutoMapper map a collection Property value from its Parent property

我有两个模型, Receipt.csReceiptProduct.cs 我想要实现的是从其父Receipt映射 ICollection ReceiptProducts字段,如PurchaseOrderIdReceiptId

收据.cs

public class Receipt
    {
        public Guid Id { get; set; }
        public string Reference { get; set; }
        public string PurchaseOrderId { get; set; }
        public virtual ICollection<ReceiptProduct> ReceiptProducts { get; set; }
    }

收据产品.cs

public class ReceiptProduct
    {
        public Guid Id { get; set; }
        public string ReceiptId { get; set; }
        public string PurchaseOrderId { get; set; }
        public string ProductName { get; set; }
        public string ProductId { get; set; }
        public string Note { get; set; }
    }
  • ReceiptProducts.ReceiptId <= Receipt.Id

  • ReceiptProducts.PurchaseOrderId <= Receipt.PurchaseOrderId

我试过下面的代码。 但我得到了错误

CreateMap<DataEntities.Receipt, BusinessEntities.Receipt>()
 .ForMember(dest => dest.ReceiptProducts.Select(x=>x.ReceiptId), automapper => automapper.MapFrom(src => src.Id));

错误: AutoMapper.AutoMapperConfigurationException: Custom configuration for members is only supported for top-level individual members on a type.

那么如何映射该集合属性值。

尝试这个。

public class ReceiptProduct
{
    public Guid Id { get; set; }
    public string ReceiptId { get; set; }
    public string PurchaseOrderId { get; set; }
    public string ProductName { get; set; }
    public string ProductId { get; set; }
    public string Note { get; set; }

    **public Receipt Receipt { get; set; }**

}

映射

CreateMap<DataEntities.ReceiptProduct, BusinessEntities.Receipt>()
.ForMember(dest => x=>x.ReceiptId, opts => opts.MapFrom(src => src.Receipt.Id))
.ForMember(dest => x=>x.PurchaseOrderId , opts => opts.MapFrom(src => src.Receipt.PurchaseOrderId))
.ForMember(dest => x=>x.Reference , opts => opts.MapFrom(src => src.Receipt.Reference ));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM