1

I use to find AutoMapper very simple to use. I am struggling with the new version. I have two types:

namespace VehicleMVC.Models
{
    public class CarModel
    {
        public int id { get; set; }
        public string make { get; set; }
        public string model { get; set; }

    }
}

and:

namespace Business
{
    public class Car
    {

        private int _id;
        private string _make;
        private string _model;

        public int id
        {
            get { return _id; }
            set { _id = value; }
        }

        public string make
        {
            get { return _make; }
            set { _make = value; }
        }

        public string model
        {
            get { return _model; }
            set { _model = value; }
        }

    }  
}

I have tried this in CarController:

public CarController()
        {
            service = new Service.Service();
            //Mapper.Initialize(cfg => cfg.CreateMap<Business.Car,CarModel>());
            //Mapper.Initialize(cfg => cfg.CreateMap<List<CarModel>, List<Business.Car>>());
            var config = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap<Business.Car, CarModel>();
            }       );
            config.AssertConfigurationIsValid();

        }

private CarModel getCarModel(Business.Car BusinessCar)
        {
            CarModel CarModel = AutoMapper.Mapper.Map<CarModel>(BusinessCar);
            return CarModel;
        }

The error I get is: An exception of type 'System.InvalidOperationException' occurred in AutoMapper.dll. Additional information: Mapper not initialized. Call Initialize with appropriate configuration. but was not handled in user code. What is wrong?

w0051977
  • 15,099
  • 32
  • 152
  • 329

1 Answers1

5

Once you have created your configuration, you must initialize the mapper with it:

var config = new MapperConfiguration(cfg =>
{
     cfg.CreateMap<Business.Car, CarModel>();
};

config.AssertConfigurationIsValid();
Mapper.Initialize(config);
stuartd
  • 70,509
  • 14
  • 132
  • 163
  • Could you take a look at my other question here: http://stackoverflow.com/questions/38378459/map-configuration-or-unsupported-mapping ? – w0051977 Jul 14 '16 at 15:40