慕仙森
我找到了解决方案。我用过ITypeConverter// Converts timestamp to DateTimepublic class DateTimeConverter : ITypeConverter<long?, DateTime?>{ private readonly DateTime _epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); public DateTime? Convert(long? source, DateTime? destination, ResolutionContext context) { if (!source.HasValue) return null; return _epoch.AddSeconds(source.Value); }}// Converts DateTime to Timestamppublic class TimeStampConverter : ITypeConverter<DateTime?, long?>{ private readonly DateTime _epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); public long? Convert(DateTime? source, long? destination, ResolutionContext context) { if (source == null) return null; var result = (long)(source - _epoch).Value.TotalSeconds; return result; }}我在startup.cs中创建了一个像这样的地图AutoMapper.Mapper.Initialize(x => { x.CreateMap<long?, DateTime?>().ConvertUsing<DateTimeConverter>(); x.CreateMap<DateTime?, long?>().ConvertUsing<TimeStampConverter>(); });我在项目中使用了这两个类,效果很好。这可能会帮助尝试实现同一目标的任何人。