神奇的AutoMapper
AutoMapper3.3.1自动转换string to DateTime时候报错:
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
而且这个在开发环境是没问题的,在生成环境才报错,从怀疑数据库到怀疑发布文件覆盖问题,最后查阅日志才发现是AutoMapper的问题。
Google后发现可能是平台问题(问题来源),开发环境各种.net framework的版本,生产环境.net 4.0,最后解决方案见链接:
Converter
public class StringToDateTimeConverter: ITypeConverter<string, DateTime> { public DateTime Convert(ResolutionContext context) { object objDateTime = context.SourceValue; DateTime dateTime; if (objDateTime == null) { return default(DateTime); } if (DateTime.TryParse(objDateTime.ToString(), out dateTime)) { return dateTime; } return default(DateTime); } }
//配置
CreateMap<string, DateTime>().ConvertUsing<StringToDateTimeConverter>();