Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying out AutoMapper on a Silverlight project.

I am a newbie on AutoMapper and actually just got introduced to it yesterday.

My Sample's data communication pattern goes thus: RDBM => ORM => Repository => DTO and vice versa.

Having done everything and getting to my server side WCF class for transmission to client I seem to be lacking there.

Manual Mapping:
public IEnumerable<UserPM> GetByID(int userID)
    var user = _repo.GetByID(userID);
    return new UserPM()
        UserName = user.UserName,
        FirstName = user.FirstName
        LastName = user.LastName

public IEnumerable<UserPM> GetByID(int userID) // Conversion error except for public IEnumerable<User> GetByID(int userID)
    var user = _repo.GetByID(userID);
    AutoMapper.Mapper.CreateMap<User, UserPM>();
    return user;

Here is the issue. I get an error saying User can not be implicitly converted to UserPM. My repository at work there. The reason
a Presentation Model is to filter your data for specific requests... Am I wrong here... Please help.
Updated 19-Aug-11 6:13am

1 solution

I beleave you need to use the ForMember method
          .ForMember(dest => dest.UserName, o => o.MapFrom(src => src.UserName)
          .ForMember(dest => dest.FirstName, o => o.MapFrom(src => src.FirstName)
          .ForMember(dest => dest.LastName, o => o.MapFrom(src => src.LstName);

A short description about doing projections in AutoMapper[^]
Share this answer
Member 7993759 19-Aug-11 12:51pm    
Is that not too much work than the manual mapping? Could you please explain more the benefit?

After reading the link above, what I saw there is just like "joining tables". I was told it could map DTOs to Entities which I have seen blogs about(but not really detailed).
I just want to know more about
Simon Bang Terkildsen 19-Aug-11 13:02pm    
well you should only call CreateMap once for each projection you have.
you'll use UserPM userPM = Mapper.Map<User, UserPM>(user); in order to actually do the projection.

so CreateMap should not be in your GetByID method but only run once in the lifetime of your application.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900