Ayobamilaye
Ayobamilaye

Reputation: 1573

ASP.NET Core Web API - How to resolve AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping

In ASP.NET Core-6 Web API, I am using Identity DB Context. I have these two models:

public class ApplicationUser : IdentityUser
{
    public string MobileNumber { get; set; }

    [DefaultValue(false)]
    public bool? IsAdmin { get; set; }

    public virtual Merchant Merchant { get; set; }
}


public class Merchant
{
    public Guid Id { get; set; }
    public string UserId { get; set; }
    public string MerchantName { get; set; }
    public string AccountNumber { get; set; }

    public virtual ApplicationUser User { get; set; }
}

Kindly note that UserName and Email are already in the IdentityUser. So, I have this DTO.

DTO:

public class MerchantCreateDto
{
    public string MerchantName { get; set; }
    public string AccountNumber { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    public string MobileNumber { get; set; }
}

I am using AutoMapper. So I dd the mapping this way:

CreateMap<MerchantCreateDto, Merchant>().ReverseMap();

Finally, I have this for the insert.

public async Task<Response<string>> CreateMerchantAsync(MerchantCreateDto model)
{
    var response = new Response<string>();
    using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
    {
            var user = _mapper.Map<ApplicationUser>(model);
            user.MobileNumber = model.MobileNumber;
            user.UserName = model.UserName;
            user.Email = model.Email;
            user.IsAdmin = true;
            var merchantPassword = "@Password";
            var result = await _userManager.CreateAsync(user, merchantPassword);
            if (result.Succeeded)
            {
                var merchantUser = _mapper.Map<Merchant>(model);
                merchantUser.UserId = user.Id;
                merchantUser.MerchantName = model.MerchantName;
                merchantUser.CreatedBy = _currentUserService.UserName;

                await _unitOfWork.AdminMerchants.InsertAsync(merchantUser);
                await _unitOfWork.Save();
                response.StatusCode = (int)HttpStatusCode.Created;
                response.Successful = true;
                response.Message = "Merchant created successfully!";
                transaction.Complete();
                return response;
            }
        response.StatusCode = (int)HttpStatusCode.BadRequest;
        response.Message = "No such data";
        response.Successful = false;
        return response;
    };
}

When I submitted the Insert (POST Request) using POSTMAN, I got this error:

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

Mapping types:
MerchantCreateDto -> ApplicationUser
MerchantCreateDto -> Identity.ApplicationUser
   at lambda_method327(Closure , Object , ApplicationUser , ResolutionContext )
   at AdminMerchantsService.CreateMerchantAsync(MerchantCreateDto model) 

All other mappings are working except this.

How do I resolve it?

Thanks

Upvotes: 0

Views: 202

Answers (1)

Rena
Rena

Reputation: 36605

The error message is very clear that you just need add type map configuration like below:

CreateMap<MerchantCreateDto, Merchant>().ReverseMap();
CreateMap<MerchantCreateDto, ApplicationUser>().ReverseMap();  //add this....

Upvotes: 1

Related Questions