【发布时间】: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);
在映射我的firstEntity 和firstDto 之前如下:
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"
}
问题是 CreatorId 和 LastModifierId 的 Guid 字段对于 firstEntity 保持相同(如预期的那样),但 SecondEntities 中元素的这些字段获取它们的默认值。
这可能是什么原因?我怎样才能防止这种情况发生?
【问题讨论】:
-
你能添加一些 json 风格的表示你现在得到的和你期望的吗?甚至是调试窗口的屏幕截图,显示你现在得到的结果,圈出问题数据并描述它应该是什么。理解这个问题有点困难。听起来您的抱怨是 automapper 正在创建一个 List
与 List 具有相同数量的条目,但 Id 和 Name 的所有值都是默认值,而不是从 List 中的条目填充的值 -
我已经添加了更多解释@CaiusJard
-
研究
AutoMapper.Collection.
标签: c# automapper .net-5