I have two classes being mapped. The source class has a DateTime
property that gets mapped to a destination property of type long
(DateTime.Ticks
), they are UpdateDt
and UpdateDtTicks
respectively.
When I use Automapper to map these two classes my UpdateDtTicks
property automaticly gets the value from the UpdateDt
property, even though the property names are not the same and I have not explicitly set the mapping for this property.
Is Automapper setting the property automatically because the property names only differ at the end? If not why is this happening as it is unexpected behavior.
Please see the code below:
static void Main(string[] args)
{
Configuration.Configure();
var person = new OrderDto
{
OrderId = 999,
MyDate = new DateTime(2015, 1, 1, 4, 5, 6)
};
var orderModel = Mapper.Map<OrderModel>(person);
Console.WriteLine(new DateTime(orderModel.MyDateTicks.Value));
Console.ReadLine();
}
public class OrderDto
{
public int OrderId { get; set; }
public DateTime MyDate { get; set; }
}
public class OrderModel
{
public int OrderId { get; set; }
public long? MyDateTicks { get; set; }
}
public class Configuration
{
public static void Configure()
{
Mapper.CreateMap<OrderDto, OrderModel>();
}
}
The result in the console:
And a watch:
MyDate
in the destination object, findsMyDate
withTicks
postfix, and tries ifMyDate.Ticks
makes sense. In this case, this is a completely valid expression. I'm looking for this feature in the documentation but that seems to be what's happening. Have you tried renaming it for example toMyDateTiicks
? - MarioDSMyDateTiicks
and it does not get mapped. I also triedMyDatesTicks
and it also does not get mapped. So it looks like a combination of the property name and the fact thatTicks
are in the property name. - Tjaart van der Walt