I have classes to map, but they don't have default constructors, and I don't want them to have. This is because I only map to/from already existing objects.
public class Order
{
public string OrderName { get; set; }
public Order(string name)
{
this.OrderName = name;
}
}
public class OrderProcessor
{
private IService service;
public string OrderName { get; set; }
public OrderProcessor(IService service)
{
this.service = service;
Mapper.Initialize(config => config.CreateMap<Order, OrderProcessor>());
}
public void Init()
{
var order = this.service.GetOrder();
// this works
Mapper.Map(order, this);
// this fails
Mapper.Configuration.AssertConfigurationIsValid();
}
}
AutoMapper.AutoMapperConfigurationException : Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
Order -> OrderProcessor (Destination member list)
No available constructor.
at Test() in Tests.cs:line
How to make configuration assert pass and why it fails when I don't want to create new objects?
I also stumbled onto this problem after recently upgrading from Automapper 4.x to 6.x.
You need to tell AutoMapper that you don't intend to have it construct the destination type by calling CreateMap<TSource, TDest>().DisableCtorValidation()
.
As per the method documentation:
// Summary:
// Disable constructor validation. During mapping this map is used
// against an existing destination object and never constructed itself.
//