【发布时间】:2017-10-30 15:13:13
【问题描述】:
我目前在后端使用自动映射器将对象映射到模型。 我最近决定使用以下代码来处理我所有的时区转换:
cfg.CreateMap<DateTime?, DateTime?>()
.ProjectUsing(i => DbFunctions.AddHours(i, offset.Hours));
cfg.CreateMap<DateTime, DateTime>()
.ProjectUsing(i => DbFunctions.AddHours(i, offset.Hours).Value);
Object.ProjectTo<ObjectModel>().SingleOrDefault();
然后它工作正常,对象被映射并转换时区
但是,当我在业务层中使用以下代码进行单个对象映射时:
Mapper.Map<Object, ObjectModel>(singleRecord);
它给出了一个错误: 此函数只能从 LINQ to Entities 调用。
堆栈跟踪:
at System.Data.Entity.DbFunctions.AddHours(Nullable`1 timeValue, Nullable`1 addValue)
at lambda_method(Closure , DateTime , DateTime , ResolutionContext )
at AutoMapper.ResolutionContext.Map[TSource,TDestination](TSource source, TDestination destination)
at lambda_method(Closure , Inventory , InventoryModel , ResolutionContext )
Mapper.Map 在特定场景中使用很重要,我也不想投影单个记录。
有没有办法解决这个问题?
【问题讨论】:
标签: c# .net entity-framework linq automapper