Automapper Project using

Viewed 3410

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 ?

2 Answers
Related