简体   繁体   English

Automapper 执行没有错误,但没有数据从源复制到目标

[英]Automapper executes without error, but no data being copied from source to destination

I have a class like this我有一个像这样的 class

public class ListOfBMTTeamMapping
{
    public class TeamMapping
    {
        public List<TeamMappings> results { get; set; }
    }
    public class TeamMappings
    {
        public int id { get; set; }
        public string areaPath { get; set; }
        public string agileReleaseTrainName { get; set; }
        public string deliveryTeamName { get; set; }
        public string keyedInTeamCode { get; set; }
        public string deliveryTeamId { get; set; }
        public bool isDeleted { get; set; }
        public string modified { get; set; }
        public string modifiedBy { get; set; }


    }
}

And here is my model class to which I need the above API class to get copied这是我的 model class 我需要将上述 API ZA2F2ED4F8EBC2CBB4C21A29DC40AB6 复制到其中

public class JsonBmtAdoMapping
{
    public int? Id { get; set; }
    public string AreaPath { get; set; }
    public string AgileReleaseTrainName { get; set; }
    public string DeliveryTeamName { get; set; }
    public string KeyedInTeamCode { get; set; }
    public string DeliveryTeamId { get; set; }
    public string IsDeleted { get; set; }
    public DateTime? Modified { get; set; }
    public string ModifiedBy { get; set; }
}

So here is my code I tried所以这是我尝试过的代码

var format = "dd/MM/yyyy"; 
var dateTimeConverter = new IsoDateTimeConverter { DateTimeFormat = format };
ListOfBMTTeamMapping.TeamMapping Results = new ListOfBMTTeamMapping.TeamMapping();
Results = JsonConvert.DeserializeObject<ListOfBMTTeamMapping.TeamMapping>(responseBody);

List<JsonBmtAdoMapping> jM = new List<JsonBmtAdoMapping>();
jM = _mapper.Map<ListOfBMTTeamMapping.TeamMapping,List<JsonBmtAdoMapping>>(Results);
int n = 10;

And here is my automapper profile这是我的自动映射器配置文件

        CreateMap<ListOfBMTTeamMapping.TeamMapping, List<JsonBmtAdoMapping>>();
        
        CreateMap<ListOfBMTTeamMapping.TeamMappings, JsonBmtAdoMapping>();

But when the code executes, Ofcourse I am getting the data in results variable without any trouble但是当代码执行时,我当然可以毫无问题地获取结果变量中的数据

But when the mapper code fires, it execute the line without any error, but no data being copied from source to my model class which is the destination但是当映射器代码触发时,它执行该行而没有任何错误,但是没有数据从源复制到我的 model class 这是目标

jM.count is always 0 when Results hold 124 rows of data当 Results 包含 124 行数据时,jM.count 始终为 0

What I did wrong我做错了什么

Your mapping from TeamMapping to List<JsonBmtAdoMapping> can't be done out of the box by AutoMapper, because your source is an object with a property that contains the list and the destination is a list on itself. AutoMapper 无法立即完成从TeamMappingList<JsonBmtAdoMapping>的映射,因为您的源是 object,其属性包含列表,而目标本身就是一个列表。

So you have to tell him, how this conversion from a single object to a list can be done.所以你必须告诉他,这个从单个 object 到列表的转换是如何完成的。 Due to the fact, that you already have a mapping for each individual item, we can use that recursively within our mapping method.由于您已经为每个单独的项目建立了映射,我们可以在映射方法中递归地使用它。

By using this mapping, it should work:通过使用此映射,它应该可以工作:

CreateMap<ListOfBMTTeamMapping.TeamMappings, JsonBmtAdoMapping>();
CreateMap<ListOfBMTTeamMapping.TeamMapping, List<JsonBmtAdoMapping>>()
    .ConvertUsing((src, _, context) => src.results.Select(context.Mapper.Map<JsonBmtAdoMapping>).ToList());

Update更新

Cause a mapper is already defined for the individual items and lists are handled automatically by AutoMapper we can even make it shorter (thanks for Lucian for the hint in the comments):因为已经为各个项目定义了映射器,并且列表由 AutoMapper 自动处理,我们甚至可以使其更短(感谢 Lucian 在评论中的提示):

CreateMap<ListOfBMTTeamMapping.TeamMappings, JsonBmtAdoMapping>();
CreateMap<ListOfBMTTeamMapping.TeamMapping, List<JsonBmtAdoMapping>>()
    .ConvertUsing((src, _, context) => context.Mapper.Map<List<JsonBmtAdoMapping>>(src.results));

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

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