I expect AutoMapper (3.3.0) to not automatically allow string conversions -> DateTime, even if the string is in a well-understood format. Lack of inclusion of a default string -> DateTime converter is noted (albeit four years ago) in a comment by library author Jimmy Bogard in response to this StackOverflow answer: https://stackoverflow.com/a/166778/
However, I have a .NET Fiddle, which seems to suggest that AutoMapper can handle this default mapping: https://dotnetfiddle.net/dDtUGx
In this example, the Zing property is mapped from string in Foo to a DateTime string in a string without specifying a custom mapping or resolver.
However, when this code works in my test solution modules (using the same version of AutoMapper), it throws the exception that I expect:
AutoMapper.AutoMapperMappingExceptionMissing type map configuration or unsupported mapping. Mapping types: String -> DateTime System.String -> System.DateTime Destination path: Bar.Zing Source value: Friday, December 26, 2014
What causes this inconsistent behavior?
For completeness, the code inside the .NET script is reproduced here:
using System; using AutoMapper; public class Program { public static void Main() { var foo = new Foo(); foo.Zing = DateTime.Now.ToLongDateString(); Mapper.CreateMap<Foo, Bar>(); var bar = Mapper.Map(foo, new Bar()); Console.WriteLine(bar.Zing); } public class Foo { public string Zing { get; set; } } public class Bar { public DateTime Zing { get; set; } } }
Nward source share