简体   繁体   English

使用 Automapper 映射嵌套集合

[英]Mapping a nested collections using Automapper

I have been trying to map my entities to my viewmodels with AutoMapper.我一直在尝试使用 AutoMapper 将我的实体映射到我的视图模型。 And faced problems with nested collection mapping.并面临嵌套集合映射的问题。

The Source来源

public class Consignment
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public ICollection<ConsignmentLine> ConsignmentLines { get; set; }
    public ICollection<ConsignmentDocument> ConsignmentDocuments { get; set; }
}

public class ConsignmentLine
{
    public Guid Id { get; set; }
    public Guid ConsignmentId { get; set; }
    public ICollection<ConsignmentDocument> ConsignmentDocuments { get; set; }
}

public class ConsignmentDocument
{
    public Guid Id { get; set; }
    public Guid ConsignmentId { get; set; }
    public Guid ConsignmentLineId { get; set; }
    public string DocumentName { get; set; }
}

public class ConsignmentLineViewModel
{
    public Guid Id { get; set; }
    public Guid ConsignmentId { get; set; }
    public ICollection<ConsignmentDocumentViewModel> ConsignmentDocuments { get; set; }
}
 
public class ConsignmentDocumentViewModel
{
    public Guid Id { get; set; }
    public Guid ConsignmentId { get; set; }
    public Guid ConsignmentLineId { get; set; }
    public string DocumentName { get; set; }
}

The destination目的地

public class ConsignmentDetailsViewModel
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public ICollection<ConsignmentLineViewModel> ConsignmentLines { get; set; }
    public ICollection<ConsignmentDocumentViewModel> ConsignmentDocuments { get; set; }
}

I can map consignmentDocuments for each consignment very easily but while mapping consignmentlines for each consignment i am getting an "AutoMapper Exception".我可以很容易地为每批货物映射寄售文件,但是在为每批货物映射寄售行时,我收到了“AutoMapper 异常”。 I know the exception is being generated because of each consignmentLine has it's own collection of consignmentDocuments.我知道正在生成异常是因为每个 consignmentLine 都有自己的 consignmentDocuments 集合。

Right now my automapper profile现在我的自动映射器配置文件

CreateMap<Consignment, ConsignmentDetailsViewModel>()
            .ForMember(vm => vm.consignmentLineViewModel, opt => opt.MapFrom(model => model.ConsignmentLine.ToList()))
            .ForMember(vm => vm.consignmentDocumentViews, opt => opt.MapFrom(model => model.ConsignmentDocument.ToList()));

How can I map all of them to the ConsignmentViewModel class?如何将它们全部映射到 ConsignmentViewModel 类?

Resolved the problem.解决了问题。

The solution is to create a map for ConsignmentLine to get the collection of ConsignmentDocuments.解决办法是为ConsignmentLine创建一个map来获取ConsignmentDocuments的集合。

CreateMap<Consignment, ConsignmentDetailsViewModel>()
        .ForMember(vm => vm.consignmentLineViewModel, opt => opt.MapFrom(model => model.ConsignmentLine))
        .ForMember(vm => vm.consignmentDocumentViews, opt => opt.MapFrom(model => model.ConsignmentDocument));

CreateMap<ConsignmentLine, ConsignmentLineViewModel>()
            .ForMember(vm => vm.consignmentDocumentViews, opt => opt.MapFrom(model => model.ConsignmentDocument));

If you act simply without thinking too complex in AutoMapper transactions, you can perform all your transactions.如果您在 AutoMapper 事务中不考虑太复杂而简单地采取行动,则可以执行所有事务。

Example:例子:

 CreateMap<Consignment, ConsignmentDetailsViewModel>();
 CreateMap<ConsignmentLine, ConsignmentLineViewModel>();
 CreateMap<ConsignmentDocument, ConsignmentDocumentViewModel>();
 

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

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