Steve
Steve

Reputation: 4453

Missing type map configuration or unsupported mapping in NET 6 web app

I have this error Missing type map configuration or unsupported mapping. in this line

var marketingEventEntity = _mapper.Map<MarketingEvent?>(request);

But I already configured it in my AutoMapperProfile. Here is the class

public class AutoMapperProfile : Profile
{
    public AutoMapperProfile()
    {
        CreateMap<CreateMarketingEventRequest, MarketingEvent?>();
    }
}

Below is the full code. I am using Mediatr and CQRS pattern.

    [HttpPost]
    public async Task<ActionResult> Create([FromBody] CreateMarketingEventRequest request)
    {
        try
        {
            var query = new CreateCommand(request);
            var result = await _mediator.Send(query);
            return CreatedAtAction(null, new { id = result }, result);
        }
        catch (Exception ex)
        {
            return Problem(ex.Message);
        }
    }

public class CreateCommand : IRequest<MarketingEvent>
{
    public CreateMarketingEventRequest createMarketingEventRequest { get; set; }

    public CreateCommand(CreateMarketingEventRequest createMarketingEventRequest)
    {
        this.createMarketingEventRequest = createMarketingEventRequest;
    }
}

public class CreateHandler : IRequestHandler<CreateCommand, MarketingEvent?>
{
    private readonly IMarketingEventService _marketingEventService;
    private readonly IMapper _mapper;

    public CreateHandler(IMarketingEventService marketingEventService, IMapper mapper)
    {
        _marketingEventService = marketingEventService;
        _mapper = mapper;
    }

    public async Task<MarketingEvent?> Handle(CreateCommand request, CancellationToken cancellationToken)
    {
        var marketingEventEntity = _mapper.Map<MarketingEvent?>(request);
        if(marketingEventEntity == null) return null;
        return await _marketingEventService.Create(marketingEventEntity);
    }
}

Upvotes: 1

Views: 280

Answers (1)

CF5
CF5

Reputation: 1173

Should this:

var marketingEventEntity = _mapper.Map<MarketingEvent?>(request);

Be:

var marketingEventEntity = _mapper.Map<MarketingEvent?>(request.CreateMarketingEventRequest);

Upvotes: 1

Related Questions