Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automapper projection don't support custom type converters #522

Closed
felix8 opened this issue May 14, 2014 · 4 comments
Closed

Automapper projection don't support custom type converters #522

felix8 opened this issue May 14, 2014 · 4 comments
Milestone

Comments

@felix8
Copy link

felix8 commented May 14, 2014

Backstory: as a part of an upgrade we moved from using DateTime to DateTimeOffset for our users. Idea is to keep EF definitions the same (everything stays System.DateTime) but configure AutoMapper such that we convert it to System.DateTimeOffset.

So in AutoMapperConfiguration.Configure() I've defined:

        Mapper.CreateMap<DateTime?, DateTimeOffset>().ConvertUsing<NullableDateTimeConverter>();

        Mapper.CreateMap<DateTimeOffset, DateTime?>().ConvertUsing<DateTimeOffsetToNullDateTimeConverter>();

        Mapper.CreateMap<DateTime, DateTimeOffset>().ConvertUsing<DateTimeConverter>();

        Mapper.CreateMap<DateTimeOffset, DateTime>().ConvertUsing<DateTimeOffsetConverter>();

Basically all possible scenarios where a conversion might be needed.

For example, I've defined DateTimeConverter() like so:

public class DateTimeConverter : TypeConverter<System.DateTime, System.DateTimeOffset>
{
    /// <summary>
    /// Converts data from DateTime to DateTimeOffset
    /// </summary>
    /// <param name="source"></param>
    /// <returns></returns>
    protected override System.DateTimeOffset ConvertCore(System.DateTime source)
    {
        return new System.DateTimeOffset(source);
    }
}

Now the conversions works great when I try to do (MyEntity being the EF entity):

return Mapper.Map<MyEntity, MyEntity2>(queryResult.Single())

But when I do this:

IQueryable<MyEntity2> entityQueryable = db.MyEntity.Project().To<MyEntity2>();

I get the following exception: AutoMapperException

Unable to create a map expression from System.DateTime to System.DateTimeOffset

@jbogard
Copy link
Member

jbogard commented Jun 6, 2014

I could do this with an expression-based version (it must be an expression to be passed down to EF, and effectively be converted to SQL and/or done server-side). Would that work?

@jbogard
Copy link
Member

jbogard commented Jul 8, 2014

This is in for 3.3, with a method called ProjectUsing.

@jbogard jbogard added this to the 3.3.0 milestone Jul 8, 2014
@felix8
Copy link
Author

felix8 commented Jul 8, 2014

Awesome!

I'll try it out; sorry for the delay in replying I was heads down shipping.

Thanks,

  • Arijit

On Jul 7, 2014, at 8:53 PM, "Jimmy Bogard" notifications@github.com wrote:

This is in for 3.3, with a method called ProjectUsing.


Reply to this email directly or view it on GitHub.

@jbogard jbogard closed this as completed Jul 18, 2014
@lock
Copy link

lock bot commented May 8, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators May 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants