0

I've upgraded Automapper from 4.2.1 to 5.0.0. I'm using the static API in a WebApi2 project and I'm trying to get the mapping to work, so I tried following this SO answer.

So I changed the code to the following:

public static class AutoMapping
{
    public static void Config()
    {
        Mapper.Initialize(main =>
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMissingTypeMaps = true;
                cfg.CreateMap<MyModel, MyDto>().ReverseMap();
            });
            config.AssertConfigurationIsValid();
        });
    }
}

The above is called from Global.asax.

However, I get exception:

Mapper not initialized. Call Initialize with appropriate configuration.

What is the correct way to initialize Automapper, and do I need to change all my controllers now for mapping?

EDIT1

Firstly, the code above must be:

Mapper.Initialize(cfg => 
{
    cfg.CreateMissingTypeMaps = true;
    cfg.CreateMap<MyModel, MyDto>().ReverseMap();
});
Mapper.Configuration.AssertConfigurationIsValid();

Secondly, the problem might be in the following method which I use to ignore missing properties:

public static IMappingExpression<TSource, TDestination> IgnoreUnmapped<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
{
    var typeMap = Mapper.Configuration.FindTypeMapFor<TSource, TDestination>();
    if (typeMap != null)
    {
        foreach (var unmappedPropertyName in typeMap.GetUnmappedPropertyNames())
        {
            expression.ForMember(unmappedPropertyName, opt => opt.Ignore());
        }
    }

    return expression;
}

I'm assuming 'Mapper.Configuration' is not yet configured because the above method is called within Initialize which configures the mapping.

Is there an existing method within Automapper itself which I can use instead of the above?

EDIT2

Would the following syntax work?

cfg.CreateMap<MyModel, MyDto>().ReverseMap().ForAllMembers(opt => opt.Ignore());
Community
  • 1
  • 1
Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263

2 Answers2

2

Actually your code does nothing now. You have to change it like this:

public static class AutoMapping
{
    public static void Config()
    {
        Mapper.Initialize(cfg =>
        {
            cfg.CreateMissingTypeMaps = true;
            cfg.CreateMap<MyModel, MyDto>().ReverseMap();
        });
        Mapper.AssertConfigurationIsValid();
    }
}

UPD (after EDIT1):

Try to use expression.TypeMap instead of Mapper.Configuration.FindTypeMapFor<TSource, TDestination>()

Alexey Merson
  • 414
  • 5
  • 12
0

Maybe this helps:

Setup the Configuration:

var config = new MapperConfiguration(cfg => cfg.CreateMap<Order, OrderDto>());

Then where the mapping should take place:

var mapper = new Mapper(config);
OrderDto dto = mapper.Map<OrderDto>(order);

You could also expose the 'config' as a static property, and use that in your project. There's also an alternative to create a static 'Mapper' property that is configured. Then you can use that static 'Mapper' property in your project.