3

In my project I use Entity Framework for ORM, and Dto classes for api responses. I use Automapper to map between the two.

When I need to directly fetch the Dtos from the EF queryables, I do a final ProjectTo<> on my query and everything is fine.

But there are many times that I have an already materialized object from entity framework and I need to Map it to its Dto equivalent. In this case I use Map<> instead of ProjectTo<> since I have an instance in memory and not an IQueryable that translates to SQL.

I have registered both Projection and Map when I created the Automapper profile like this:

CreateMap<UserSession, Models.UserSession>(); // used when mapping between instances
CreateProjection<UserSession, Models.UserSession>(); // used when mapping on EF LINQ expressions

That should be enough for Automapper to understand that when I use ProjectTo I want the Projection mapping and when I use Map I want the instance mapping.

But, instead, when I have a materialized UserSession object (the type registered with Entity Framework), and want to map it to a Models.UserSession object (my Dto) I get this response.

CreateProjection works with ProjectTo, not with Map.

So, how can I register both map and projection for the same types in Automapper?

Thanasis Ioannidis
  • 2,981
  • 1
  • 30
  • 50

1 Answers1

6

In this case you have to use CreateMap. CreateProjection explicitly disables Map. CreateMap allows both.

Lucian Bargaoanu
  • 3,336
  • 3
  • 14
  • 19