【问题标题】:AutoMapper - Fields in the list gets their default valuesAutoMapper - 列表中的字段获取其默认值
【发布时间】:2022-01-07 18:55:45
【问题描述】:

我有以下对象:

class FirstEntity {
    public string Id;
    public string Name;
    public List<SecondEntity> SecondEntities;

    public Guid CreatorId;
    public Guid LastModifierId;
}

class SecondEntity {
    public string Id;
    public string AnotherName;

    public Guid CreatorId;
    public Guid LastModifierId;
}

class FirstDto {
    public string Id;
    public string Name;
    public List<SecondDto> SecondEntities;
}

class SecondDto {
    public string Id;
    public string AnotherName;
}

我的映射如下:

CreateMap<FirstEntity, FirstDto>()
.ReverseMap()
.ForAllMembers(opt => opt.Condition((src, dest, srcMember) => srcMember != null)); 
     
CreateMap<SecondEntity, SecondDto>()
.ReverseMap()
.ForAllMembers(opt => opt.Condition((src, dest, srcMember) => srcMember != null));

我的映射如下:

var firstEntity = _firstEntityDal.SingleOrDefault(w => w.Id == someId);
automapper.Map(firstDto, firstEntity);

在映射我的firstEntityfirstDto 之前如下:

firstEntity{
  Id: "54bfbfaa-1a8a-4239-a777-f504f90eaa79",
  Name: "First Entity",
  SecondEntities: [
    {
        Id: "191ca292-ff5b-4dae-881b-c883641296f4"
        AnotherName: "SecondEntity 1",
        CreatorId: "76af4a15-c73c-4d0a-a5ff-8b44a50f781b",
        LastModifierId: "7971e3a5-e6ca-4b28-b631-0928b7455167"
        },
        {
        Id: "745d4e38-e82b-47cb-95fa-f6a522d6eb57"
        AnotherName: "SecondEntity 2",
        CreatorId: "76af4a15-c73c-4d0a-a5ff-8b44a50f781b",
        LastModifierId: "7971e3a5-e6ca-4b28-b631-0928b7455167"
        },
        {
        Id: "685415c7-2d68-485c-a9de-1433e17e6ebb"
        AnotherName: "SecondEntity 3",
        CreatorId: "76af4a15-c73c-4d0a-a5ff-8b44a50f781b",
        LastModifierId: "7971e3a5-e6ca-4b28-b631-0928b7455167"
        }
    ],
    CreatorId: "76af4a15-c73c-4d0a-a5ff-8b44a50f781b",
    LastModifierId: "7971e3a5-e6ca-4b28-b631-0928b7455167"
}

firstDto{
  Id: "54bfbfaa-1a8a-4239-a777-f504f90eaa79",
  Name: "First Entity",
  SecondEntities: [
    {
        Id: "191ca292-ff5b-4dae-881b-c883641296f4"
        AnotherName: "SecondEntity 1"
        },
        {
        Id: "745d4e38-e82b-47cb-95fa-f6a522d6eb57"
        AnotherName: "SecondEntity 2"
        },
        {
        Id: "685415c7-2d68-485c-a9de-1433e17e6ebb"
        AnotherName: "SecondEntity 3"
        }
    ]
}

映射firstEntity后变成这样:

   firstEntity {
      Id: "54bfbfaa-1a8a-4239-a777-f504f90eaa79",
      Name: "First Entity",
      SecondEntities: [
        {
            Id: "191ca292-ff5b-4dae-881b-c883641296f4"
            Name: "SecondEntity 1",
            CreatorId: "00000000-0000-0000-0000-000000000000",
            LastModifierId: "00000000-0000-0000-0000-000000000000"
            },
            {
            Id: "745d4e38-e82b-47cb-95fa-f6a522d6eb57"
            Name: "SecondEntity 2",
            CreatorId: "00000000-0000-0000-0000-000000000000",
            LastModifierId: "00000000-0000-0000-0000-000000000000"
            },
            {
            Id: "685415c7-2d68-485c-a9de-1433e17e6ebb"
            Name: "SecondEntity 3",
            CreatorId: "00000000-0000-0000-0000-000000000000",
            LastModifierId: "00000000-0000-0000-0000-000000000000"
            }
        ],
        CreatorId: "76af4a15-c73c-4d0a-a5ff-8b44a50f781b",
        LastModifierId: "7971e3a5-e6ca-4b28-b631-0928b7455167"
    }

问题是 CreatorIdLastModifierId 的 Guid 字段对于 firstEntity 保持相同(如预期的那样),但 SecondEntities 中元素的这些字段获取它们的默认值。

这可能是什么原因?我怎样才能防止这种情况发生?

【问题讨论】:

  • 你能添加一些 json 风格的表示你现在得到的和你期望的吗?甚至是调试窗口的屏幕截图,显示你现在得到的结果,圈出问题数据并描述它应该是什么。理解这个问题有点困难。听起来您的抱怨是 automapper 正在创建一个 List 与 List 具有相同数量的条目,但 Id 和 Name 的所有值都是默认值,而不是从 List 中的条目填充的值
  • 我已经添加了更多解释@CaiusJard
  • 研究AutoMapper.Collection.

标签: c# automapper .net-5


【解决方案1】:

映射firstEntity后变成这样:

哦,我明白了.. 我想问题是您希望 AutoMapper 会看到您有一个 ID 为 191ca292-ff5b-4dae-881b-c883641296f4 的现有 SecondEntity,并且您有一个 ID 为 191ca292-ff5b-4dae-881b-c883641296f4 的传入 SecondDto,因此 AutoMapper 应该查找按 ID 在列表中现有的 SecondEntity,然后仅使用来自 SecondDto 的新值更新现有实例 ..

..据我所知,AutoMapper 没有任何自动功能。您看到您的 Guid 重置为 00000...,因为基本上 AutoMapper 会清除列表并使用根据 SecondDto 中的信息创建的新 SecondEntity 对象(缺少创建者/修改者 ID)

一种方法是在 SecondEntities 列表上设置一个 Ignore,以便 AutoMapper 不理会它,然后使用循环进行查找,并使用 AutoMapper 映射您想要的直接实例,例如

foreach(var sd in myFirstDto.SecondEntitites)
  automapper.Map(sd, myFirstEntity.SecondEntities.Single(se => se.Id == sd.Id));

您还可以创建自定义转换器并告诉 automapper 使用它,请参阅Map list to existing list in Automapper using a key 了解更多信息

【讨论】:

  • 哦,好的。是的,我期待列表元素中的 guid 保持不变,就像在外部对象中一样。现在,我在没有自动映射器解决方案的情况下解决了我的问题,但这无论如何都会有用。谢谢! :)
猜你喜欢
  • 1970-01-01
  • 2016-12-28
  • 2011-12-30
  • 2017-01-28
  • 1970-01-01
  • 2021-03-08
  • 2012-10-24
  • 1970-01-01
  • 2010-11-21
相关资源
最近更新 更多