Search code examples
c#automapper-9

Automapper 9 Configuration


In previous versions of AutoMapper I used to be able to configure AutoMapper like this:

public static class AutoMapperFactory
{
    public static IConfigurationProvider CreateMapperConfiguration()
    {
        var config = new MapperConfiguration(cfg =>
        {
            //Scan *.UI assembly for AutoMapper Profiles
            var assembly = Assembly.GetAssembly(typeof(AutoMapperFactory));

            cfg.AddProfiles(assembly);

            cfg.IgnoreAllUnmapped();
        });

        return config;
    }
}

Now, the line that says cfg.AddProfiles(assembly) is giving me the error: Argument 1: cannot convert from 'System.Reflection.Assembly' to 'System.Collections.Generic.IEnumerable<AutoMapper.Profile>

How can I get an IEnumerable<AutoMapper.Profile> to pass as a parameter for AddProfiles?


Solution

  • You can use the addMaps, instead of addProfile, like this:

    public static class AutoMapperFactory
    {
        public static IConfigurationProvider CreateMapperConfiguration()
        {
            var config = new MapperConfiguration(cfg =>
            {
                //Scan *.UI assembly for AutoMapper Profiles
                var assembly = Assembly.GetAssembly(typeof(AutoMapperFactory));
    
                cfg.AddMaps(assembly);
    
                cfg.IgnoreAllUnmapped();
            });
    
            return config;
        }
    }
    

    As stated in the documentation:

    Configuration inside a profile only applies to maps inside the profile. Configuration applied to the root configuration applies to all maps created.

    And can be created as classes with specific type maps.