簡體   English   中英

AutoMapper 不映射子實體列表

[英]AutoMapper not mapping sub entity list

我映射了一個 model,它也有一個子列表子 map。 但是,調用map后,子列表沒有被映射? 我正在使用AutoMapper 9.0.0AutoMapper.Extensions.Microsoft.DependencyInjection 7.0.0 (注意這是 package 列表中的父節點。

我有如下(為簡潔起見):

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; }
}

我的映射如下;

        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>>();

然后我在我的 controller 中的 map 如下;

        var response = await _agreementService.FindAsync(criteria);

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

誰能看到我在這里做錯了什么?

映射 collections 會阻止集合屬性映射工作。 請參閱下面的工作測試:

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