Reputation: 1138
I am using AutoMapper in my ASP.NET Core project. When I want to map from DTO type into model type I am getting error:
"Type is not valid in the given context".
This is my mapper configuration:
protected AutoMapperOrderConfiguration(string profileName) : base(profileName)
{
CreateMap<OrderDTO, Order>();
CreateMap<Order, OrderDTO>();
}
Here is the code that produces the error:
public void Add(OrderDTO item)
{
var model = _mapper.Map(OrderDTO, Order)(item);
_orderRepository.Add(model);
}
Here I want to add new DTO item and then I want to transform it to base model. Then I get error.
public IActionResult Create([FromBody] OrderDTO item)
{
if (item.OrderType == "" || item.ServiceType=="")
{
return BadRequest();
}
_orderDTORepository.Add(item);
return CreatedAtRoute("GetOrder", new { id = item.OrderId }, item);
}
Upvotes: 2
Views: 1904
Reputation: 58898
Shouldn't the mapping call be:
_mapper.Map<Order>(item);
Now we define that we wish to map item
to the Order
class.
Upvotes: 1