【发布时间】:2017-03-20 21:07:38
【问题描述】:
我在我的 ASP.NET Core 项目中使用 AutoMapper。当我想从 DTO 类型映射到模型类型时出现错误:
“类型在给定的上下文中无效”。
这是我的映射器配置:
protected AutoMapperOrderConfiguration(string profileName) : base(profileName)
{
CreateMap<OrderDTO, Order>();
CreateMap<Order, OrderDTO>();
}
这是产生错误的代码:
public void Add(OrderDTO item)
{
var model = _mapper.Map(OrderDTO, Order)(item);
_orderRepository.Add(model);
}
在这里我想添加新的 DTO 项目,然后我想将其转换为基本模型。然后我得到错误。
public IActionResult Create([FromBody] OrderDTO item)
{
if (item.OrderType == "" || item.ServiceType=="")
{
return BadRequest();
}
_orderDTORepository.Add(item);
return CreatedAtRoute("GetOrder", new { id = item.OrderId }, item);
}
【问题讨论】:
-
我的映射器工作,我可以从模型映射到 DTOModel。反向,我不能。