0

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

2
  • 1
    Error tells you, doesn't it? It says no mapping from MerchantCreateDto to ApplicationUser. Second, don't use AutoMapper to map Dtos to Entities, it screams for trouble Commented Jul 30, 2022 at 0:49
  • @Tseng - What's the way out Commented Jul 30, 2022 at 1:10

1 Answer 1

1

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....
Sign up to request clarification or add additional context in comments.

Comments

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.