我一直在努力使用automapper和嵌套对象.我收到以下错误:
Missing type map configuration or unsupported mapping. AdminSchema -> Schema
但这对我来说似乎不对,因为我确实配置了以下映射:
Mapper.CreateMap<Schema,AdminSchema>().ReverseMap();
它应该映射以下模型:
public class AdminSchema { [required] [display(Name = "Opeenvolgende weken")] public int ConsecutiveWeeks { get; set; } public List<AdminWorkDay> WorkDays { get; set; } public bool Delete { get; set; } } public class AdminWorkDay { public int DayOfWeekNumber { get; set; } public string StartTime { get; set; } public string EndTime { get; set; } public TimeSpan BreakTime { get; set; } public bool Delete { get; set; } }
至
public class Schema { public int Id { get; set; } public int ConsecutiveWeeks { get; set; } public List<WorkDay> WorkDays { get; set; } } public class WorkDay { public int Id { get; set; } public int DayOfWeekNumber { get; set; } public DateTime StartTime { get; set; } public DateTime EndTime { get;set; } public TimeSpan BreakTime { get; set; } }
Mapper.CreateMap<string,DateTime>().ConvertUsing<StringToDateTimeConverter>(); public class StringToDateTimeConverter : ITypeConverter<string,DateTime> { public DateTime Convert(ResolutionContext context) { var sourceDt = context.sourceValue; DateTime targetDt; if (sourceDt == null) { return default(DateTime); } return DateTime.TryParse(sourceDt.ToString(),out targetDt) ? targetDt : default(DateTime); } }
以下是映射的实现:
var workPeriod = new WorkPeriod { Schemas = Mapper.Map<List<AdminSchema>,List<Schema>>(workPeriodVm.Schemas) };
如果有人可能知道为什么它找不到我的映射,请告诉我.非常感谢.其他映射确实有效,因此一般来说,自动播放器的配置是正确的.
解决方法
显然,TSource和TDestination中的WorkDays类型不相等,而您必须在AutoMapper Nuget Package中设置相同的TSource和TDestination类型. 例如,将TSource和TDestination类型更改为AdminWorkDay for WorkDays属性,您会看到它正常工作.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。