简体   繁体   English

AutoMapper 不映射子实体列表

[英]AutoMapper not mapping sub entity list

I mapping a model across, which has a child list sub map as well.我映射了一个 model,它也有一个子列表子 map。 However, after calling the map, the sub list is not being mapped?但是,调用map后,子列表没有被映射? I am using AutoMapper 9.0.0 with AutoMapper.Extensions.Microsoft.DependencyInjection 7.0.0 (note this is the parent node in the package list.我正在使用AutoMapper 9.0.0AutoMapper.Extensions.Microsoft.DependencyInjection 7.0.0 (注意这是 package 列表中的父节点。

I have as follows (reduced for brevity):我有如下(为简洁起见):

public class Agreement 
{
    //...
    public List<Document> Documents { get; set; }
}


public class Document : Entity
{
    public string Url { get; set; }
    public string Location { get; set; }
    public string MimeType { get; set; }
    public string FileHash { get; set; }
    public float FileSize { get; set; }
    public string Notes { get; set; }
    public string Type { get; set; }
    public byte[] Data { get; set; }
}

public class AgreementDataGridOutputModel : BaseModel
{
    //...
    public List<DocumentOutputModel> Documents { get; set; }
}

public class DocumentOutputModel
{
    public int Id { get; set; }
    public string Url { get; set; }
    public string MimeType { get; set; }
    public string Notes { get; set; }
}

My Mappings are as follows;我的映射如下;

        CreateMap<Document, DocumentOutputModel>();
        CreateMap<List<Document>, List<DocumentOutputModel>>();

        CreateMap<Agreement, AgreementDataGridOutputModel>()
           .ForMember(dest => dest.AgreementType, opt => opt.MapFrom(src => src.Type.Name))
           .ForMember(dest => dest.CompanyName, opt => opt.MapFrom(src => src.Company.Name))
           .ForMember(dest => dest.Documents, opt => opt.MapFrom(src => src.Documents));

        CreateMap<List<Agreement>, List<AgreementDataGridOutputModel>>();

I then map in my controller as follows;然后我在我的 controller 中的 map 如下;

        var response = await _agreementService.FindAsync(criteria);

        var output = _mapper.Map<IList<Agreement>,IList<AgreementDataGridOutputModel>>(response.Result);

Can anyone see what I am doing wrong here please?谁能看到我在这里做错了什么?

Mapping collections prevent the collection properties maps from working.映射 collections 会阻止集合属性映射工作。 See a working test below:请参阅下面的工作测试:

using AutoMapper;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            var list = new List<ParentSource> {
                new ParentSource {
                    Id =1,
                    Name = "My name",
                    MyList = new List<ChildSource> {
                        new ChildSource { Id = 1, Name = "Child name" }
                    }
                }
            };
            var conf = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<ParentSource, ParentTarget>();
                cfg.CreateMap<ChildSource, ChildTarget>();
                /*
                 * This line prevents the list mappings from working...
                 */
                // cfg.CreateMap<List<ChildSource>, List<ChildTarget>>();
            });

            var mapper = new Mapper(conf);
            var targets = mapper.Map<List<ParentSource>, IList<ParentTarget>>(list);
            Console.WriteLine(JsonSerializer.Serialize(targets));
            // Output: [{"Id":1,"Name":"My name","MyList":[{"Id":1,"Name":"Child name"}]}]
            Console.ReadLine();

        }
    }

    public class ParentSource
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<ChildSource> MyList { get; set; }
    }

    public class ChildSource
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class ParentTarget
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<ChildTarget> MyList { get; set; }
    }

    public class ChildTarget
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

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

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