我目前正在后端使用automapper将对象映射到模型。 我最近决定使用以下代码来处理所有时区转换:
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在特定场景中很重要,我也不想投影单个记录。
这有什么方法吗?
I am currently using automapper in my backend to map objects to models. I recently decided to use the following code to handle all my timezone conversions:
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();then it works fine and the object is mapped and timezone converted
However when i am using the following code in my business layer to do a single object mapping:
Mapper.Map<Object, ObjectModel>(singleRecord);It Gives an error : This function can only be invoked from LINQ to Entities.
stack trace :
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 is important to use in specific scenarios and i also don't want to project single records.
is there a way round this ?
最满意答案
默认情况下,AutoMapper将通过编译传递给ProjectUsing并由ProjectTo使用的Expression<TFunc<TSource, TDestionation>>来构建映射Func<TSource, TDestination>>以供Map方法使用,因为通常它足够并且有效。 但不是EF规范功能。
您可以通过显式提供ProjectUsing和ConvertUsing指定要与Map方法一起使用的其他转换来覆盖该行为:
var map1 = cfg.CreateMap<DateTime?, DateTime?>(); map1.ProjectUsing(i => DbFunctions.AddHours(i, offset.Hours)); map1.ConvertUsing(i => i?.AddHours(offset.Hours)); var map2 = cfg.CreateMap<DateTime, DateTime>(); map2.ProjectUsing(i => DbFunctions.AddHours(i, offset.Hours).Value); map2.ConvertUsing(i => i.AddHours(offset.Hours));By default AutoMapper will build a mapping Func<TSource, TDestination>> to be used by Map method by compiling the Expression<TFunc<TSource, TDestionation>> passed to ProjectUsing and used by ProjectTo, because usually it's enough and works. But not with EF canonical functions.
You could override that behavior by specifying a different conversion to be used with Map method by providing explicitly both ProjectUsing and ConvertUsing:
var map1 = cfg.CreateMap<DateTime?, DateTime?>(); map1.ProjectUsing(i => DbFunctions.AddHours(i, offset.Hours)); map1.ConvertUsing(i => i?.AddHours(offset.Hours)); var map2 = cfg.CreateMap<DateTime, DateTime>(); map2.ProjectUsing(i => DbFunctions.AddHours(i, offset.Hours).Value); map2.ConvertUsing(i => i.AddHours(offset.Hours));Automapper项目使用(Automapper Project using)我目前正在后端使用automapper将对象映射到模型。 我最近决定使用以下代码来处理所有时区转换:
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在特定场景中很重要,我也不想投影单个记录。
这有什么方法吗?
I am currently using automapper in my backend to map objects to models. I recently decided to use the following code to handle all my timezone conversions:
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();then it works fine and the object is mapped and timezone converted
However when i am using the following code in my business layer to do a single object mapping:
Mapper.Map<Object, ObjectModel>(singleRecord);It Gives an error : This function can only be invoked from LINQ to Entities.
stack trace :
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 is important to use in specific scenarios and i also don't want to project single records.
is there a way round this ?
最满意答案
默认情况下,AutoMapper将通过编译传递给ProjectUsing并由ProjectTo使用的Expression<TFunc<TSource, TDestionation>>来构建映射Func<TSource, TDestination>>以供Map方法使用,因为通常它足够并且有效。 但不是EF规范功能。
您可以通过显式提供ProjectUsing和ConvertUsing指定要与Map方法一起使用的其他转换来覆盖该行为:
var map1 = cfg.CreateMap<DateTime?, DateTime?>(); map1.ProjectUsing(i => DbFunctions.AddHours(i, offset.Hours)); map1.ConvertUsing(i => i?.AddHours(offset.Hours)); var map2 = cfg.CreateMap<DateTime, DateTime>(); map2.ProjectUsing(i => DbFunctions.AddHours(i, offset.Hours).Value); map2.ConvertUsing(i => i.AddHours(offset.Hours));By default AutoMapper will build a mapping Func<TSource, TDestination>> to be used by Map method by compiling the Expression<TFunc<TSource, TDestionation>> passed to ProjectUsing and used by ProjectTo, because usually it's enough and works. But not with EF canonical functions.
You could override that behavior by specifying a different conversion to be used with Map method by providing explicitly both ProjectUsing and ConvertUsing:
var map1 = cfg.CreateMap<DateTime?, DateTime?>(); map1.ProjectUsing(i => DbFunctions.AddHours(i, offset.Hours)); map1.ConvertUsing(i => i?.AddHours(offset.Hours)); var map2 = cfg.CreateMap<DateTime, DateTime>(); map2.ProjectUsing(i => DbFunctions.AddHours(i, offset.Hours).Value); map2.ConvertUsing(i => i.AddHours(offset.Hours));
发布评论