David
David

Reputation: 699

Automapper: "Missing type map configuration or unsupported mapping."

I got an User and a UserListDto. I'm trying to map my User to UserListDto.

However I'm getting an,

Missing type map configuration or unsupported mapping.

It works when I call GetUsers() but not GetUser(int id)

UsersController:

    [HttpGet]
    public async Task<IActionResult> GetUsers()
    {
        var users = await _repo.GetUsers();

        var usersToReturn = _mapper.Map<IEnumerable<UserForListDto>>(users);

        return Ok(usersToReturn);
    }

    [HttpGet("{id}", Name = "GetUser")]
    public IActionResult GetUser(int id)
    {
        var user = _repo.GetUser(id, false);

        var userToReturn = _mapper.Map<UserForListDto>(user);

        return Ok(userToReturn);
    }

AutoMapperProfiles:

public class AutoMapperProfiles : Profile
{
    public AutoMapperProfiles()
    {
        CreateMap<User, UserForListDto>();
        CreateMap<User, UserForDetailedDto>();
        CreateMap<UserForRegisterDto, User>();
        CreateMap<HighScoreDto, HighScore>()
        .ForMember(h=>h.TimeBetweenClicksAverage, 
        m=>m.MapFrom(u=>u.TimeBetweenClicksArray.Average()));
        CreateMap<HighScore,HighScoreForReturnDto>();



    }
}

User:

public class User : IdentityUser<int>
{
    public virtual ICollection<UserRole> UserRoles { get; set; }
    public virtual ICollection<HighScore> HighScores { get; set; }
}

UserForDetailedDto:

public class UserForDetailedDto
{
    public int Id { get; set; }
    public string Username { get; set; }
}

Upvotes: 1

Views: 2174

Answers (2)

Matt U
Matt U

Reputation: 5118

It looks like there are two problems: 1) you're not awaiting the call to _repo.GetUser and 2) you're trying to map a single User to an IEnumerable<UserForListDto>.

Make sure you await _repo.GetUser and then _mapper.Map<UserForListDto>(user)

Since you're not awaiting the repo call, it's trying to map a type Task to UserListDto, which is not a configured mapping.

Upvotes: 3

slyDog
slyDog

Reputation: 49

It looks like you might be trying to map a single User object to an IEnumerable collection. In your GetUser(int id) method, try mapping it to a single UserForListDto object this way:

_mapper.Map<UserForListDto>(user)

Upvotes: 1

Related Questions