Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper throwing "No default constructor" during validation

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?

like image 514
one_mile_run Avatar asked Apr 25 '17 09:04

one_mile_run


People also ask

When should you not use AutoMapper?

If you have to do complex mapping behavior, it might be better to avoid using AutoMapper for that scenario. Reverse mapping can get very complicated very quickly, and unless it's very simple, you can have business logic showing up in mapping configuration.

How do I ignore properties in AutoMapper?

So, the AutoMapper Ignore() method is used when you want to completely ignore the property in the mapping. The ignored property could be in either the source or the destination object.

How does AutoMapper work internally?

How AutoMapper works? AutoMapper internally uses a great concept of programming called Reflection. Reflection in C# is used to retrieve metadata on types at runtime. With the help of Reflection, we can dynamically get a type of existing objects and invoke its methods or access its fields and properties.

Is AutoMapper a singleton?

Your configuration (e.g. Automapper Profiles) are singletons. That is, they are only ever loaded once when your project runs. This makes sense since your configurations will not change while the application is running.


1 Answers

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.
    //
like image 60
Andre Luus Avatar answered Sep 21 '22 16:09

Andre Luus