AutoMapper - 列表中的字段获取默认值

AutoMapper - Fields in the list gets their default values

我有以下对象:

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

问题是 Guid 字段 CreatorIdLastModifierId 对于 firstEntity 保持不变(正如预期的那样),但是 SecondEntities 中元素的这些字段获得了它们的默认值.

这可能是什么原因?我该如何防止这种情况?

After mapping firstEntity turns to this:

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

..据我所知,AutoMapper 没有任何自动功能。您看到您的 Guids 重置为 00000...,因为基本上 AutoMapper 清除了 List 并用从 SecondDto 中的信息创建的新 SecondEntity 对象填充它(缺少 creator/modifier 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 了解更多信息