1

I am trying to use AutoMapper in my Asp.NET 5 Core project.

In the Startup file of my project, I added the following

Type[] types = all scannable types...

services.AddAutoMapper((serviceProvider, MapperExpression) =>
{
    // Not sure if this is needed
    MapperExpression.ConstructServicesUsing(t => serviceProvider);

    // Manually add the mapping without using a profile for now..
    MapperExpression.CreateMap(typeof(User), typeof(UserViewModel));

}, types);

From the above, I am expected to be able to map a UserViewModel to User. But instead, I am getting the following error

AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types: Object -> User System.Object - User

Here is how I am calling AutoMapper from a controller

[ApiController]
public class UsersController : ControllerBase
{
    private readonly IMapper _mapper;

    public UsersController(IMapper mapper)
    {
        _mapper = mapper;
    }

    public async Task<ActionResult<DisplayUserViewModel>> Create([FromBody] UserViewModel viewModel)
    {
        User model = _mapper.Map<User>(viewModel);

        //...
    }
}

Here are my models

public class UserViewModel
{
    [Required]
    public string Password { get; set; }

    [Required]
    public string Email { get; set; }

    [Required]
    public string UserName { get; set; }

    [Required]
    public string FirstName { get; set; }
    public string MiddleName { get; set; }

    [Required]
    public string LastName { get; set; }
}

public class User : IdentityUser<int>
{
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
}

How can I correctly add AutoMapper service to my .Net 5 core project?

1 Answer 1

2

2 things are needed:

  1. In the Startup.cs file:

    services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
    
  2. You may create a folder for your mappings and call it MappingProfiles or anything you want. And then add your mapping profile classes, note, the class should inherit from Profile object of the AutoMapper namespace and do the mappings in the constructor.

     public class UserProfile : Profile
     {
         CreateMap<UserViewModel, User>();
     }
    

In your case, it doesn't work because you are doing the wrong mapping direction. You need to make it opposite.

Change to:

MapperExpression.CreateMap(typeof(UserViewModel), typeof(User));
Sign up to request clarification or add additional context in comments.

3 Comments

Are you able to tell my why my code isn't working? my code is creating the maps without profile
@Jay I updated my answer. Look at the end.
Thank you! Indeed I had the reverse mapping otherwise my code is correct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.