c# - AutoMapper 不映射子实体列表
问题描述
我映射了一个模型,它也有一个子列表子图。但是,调用地图后,子列表没有被映射?我正在使用AutoMapper 9.0.0
with AutoMapper.Extensions.Microsoft.DependencyInjection 7.0.0
(注意这是包列表中的父节点。
我有如下(为简洁起见):
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>>();
然后我在我的控制器中映射如下;
var response = await _agreementService.FindAsync(criteria);
var output = _mapper.Map<IList<Agreement>,IList<AgreementDataGridOutputModel>>(response.Result);
谁能看到我在这里做错了什么?
解决方案
映射集合会阻止集合属性映射工作。请参阅下面的工作测试:
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; }
}
}
推荐阅读
- delphi - 如何在 Firemonkey 中将 onFinish evant 添加到 TAnimate
- python - 用 selenium 下载 facebook 视频
- r - 在简单的 Shiny 示例中使用 group_by 和 checkboxGroupInput
- azure - Microsoft Azure 自定义域检查自定义域添加操作失败
- prolog - Prolog 问题:变量有问题(如果我需要它们)
- reactjs - 在 reactjs 中提交表单后,如何获取数据或将数据从一个页面传递到另一个页面
- bash - 用于检查文件是否存在于另一个目录(包括子目录)中的 Bash 脚本
- r - 如何按因子计算线的斜率
- algorithm - Big-O 表示法:我是否需要使用归纳法,是否首选限制?
- python - 使用 Python 从 Grakn 获取数据