2016-07-14 7 views
1

私はAutoMapperを非常に使いやすく見つけるために使います。私は新しいバージョンに苦労しています。System.InvalidOperationException 'がAutoMapper.dllで発生しました。追加情報:Mapperは初期化されていません

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

    } 
} 

と::私はCarControllerでこれを試してみました

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; } 
     } 

    } 
} 

:私は2つのタイプがあります

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; 
     } 

私が手にエラーがある:タイプ'System.InvalidOperationException' occurred in AutoMapper.dll. Additional information: Mapper not initialized. Call Initialize with appropriate configuration.の例外が、処理されませんでしたユーザーコードでなにが問題ですか?

+0

を – stuartd

+0

は、あなたがもらえますコメントされたコードと同じエラー? – Rhumborl

+0

@stuartdでMapper.Initialize()を呼び出す必要があります。https://github.com/AutoMapper/AutoMapper/wiki/Getting-startedには、初期設定またはマッパー設定を呼び出す必要があります。 – w0051977

答えて

4

あなたの設定を作成したら、あなたはそれでマッパー初期化する必要があります:あなたは、あなたが作成した設定を呼び出す必要がありInitialize`、 `への呼び出しをコメントアウトしまし

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

config.AssertConfigurationIsValid(); 
Mapper.Initialize(config); 
+0

ここで私の他の質問をご覧ください:http://stackoverflow.com/questions/38378459/map-configuration-or-unsupported-mapping? – w0051977

関連する問題