Because my Issue on Github was closed I am opening a thread here.
GitHub issue
Source type:
public class ATest1
{
public int Id { get; set; }
}
Destination type:
public class ATest2
{
public int Id { get; set; }
public DateTime DateCreated { get; set; }
}
Mapping configuration:
CreateMap<ATest, ATest2>()
.ForMember(x => x.DateCreated, opt => opt.MapFrom((src, dest)
=>
{
return dest.Id <= 0 ? DateTime.UtcNow : dest.DateCreated;
}))
.EqualityComparison((source, destination) => source.Id == destination.Id);
Versions:
Automapper: 12.0.1
Automapper.Collection: 9.0.0
EF Core: 8.0.3
Expected behavior
This mapping was provided for mapping List of entites defined above. My expectation is that when I will check if it is existing entity or not (has database Id or not), based on this condition I can leave the DateCreated value of existing item as it is and just set the DateCreated value for new items.
Actual behavior
But after mapping the lists, existing items DateCreated value is always set to DateTime.MinValue. I debugged and I saw that I am not getting the destination object values for DateTime.
I already tried the .Condition()
and .PreCondition()
methods but the same happens.
See images below:
Image 1
Image 2
Image 3
Image 4
Is this a bug or am I doing something wrong ?
It would be good if someone can explain the behaviour here.
Veiz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.