I have following objects:
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;
}
My mapping is as follows:
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));
I am mapping like below:
var firstEntity = _firstEntityDal.SingleOrDefault(w => w.Id == someId);
automapper.Map(firstDto, firstEntity);
Before mapping my firstEntity
and firstDto
is like below:
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"
}
]
}
After mapping firstEntity
turns to this:
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"
}
The problem is Guid fields CreatorId
and LastModifierId
remains the same for firstEntity
(as expected) but these fields for the elements in SecondEntities
gets their default values.
What can be the reason for this? How can I prevent this?