NET 6 Web 应用程序中缺少类型映射配置或映射不受支持
Missing type map configuration or unsupported mapping in NET 6 web app
我在这一行有这个错误Missing type map configuration or unsupported mapping.
var marketingEventEntity = _mapper.Map<MarketingEvent?>(request);
但我已经在我的 AutoMapperProfile 中配置了它。这是 class
public class AutoMapperProfile : Profile
{
public AutoMapperProfile()
{
CreateMap<CreateMarketingEventRequest, MarketingEvent?>();
}
}
下面是完整的代码。我正在使用 Mediatr 和 CQRS 模式。
[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);
}
}
应该这样:
var marketingEventEntity = _mapper.Map<MarketingEvent?>(request);
是:
var marketingEventEntity = _mapper.Map<MarketingEvent?>(request.CreateMarketingEventRequest);
我在这一行有这个错误Missing type map configuration or unsupported mapping.
var marketingEventEntity = _mapper.Map<MarketingEvent?>(request);
但我已经在我的 AutoMapperProfile 中配置了它。这是 class
public class AutoMapperProfile : Profile
{
public AutoMapperProfile()
{
CreateMap<CreateMarketingEventRequest, MarketingEvent?>();
}
}
下面是完整的代码。我正在使用 Mediatr 和 CQRS 模式。
[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);
}
}
应该这样:
var marketingEventEntity = _mapper.Map<MarketingEvent?>(request);
是:
var marketingEventEntity = _mapper.Map<MarketingEvent?>(request.CreateMarketingEventRequest);