2017-02-06 49 views
-1

我正在使用AutoMapper來映射兩個對象。 ViewModel和Model,其中ViewModel實現InotifyPropertyChanged。我如何將模型映射到ViewModel。下面是我的方案,Automapper映射異常與模型和私有屬性

  1. 型號

    公共類型號 { 公共字符串與resultType {獲得;組; }}

  2. 視圖模型

    公共類視圖模型:屏幕 { 私人字符串_resultType;

    public string ResultType 
    { 
        get { return _resultType; } 
        set 
        { 
         _resultType = value; 
         NotifyOfPropertyChange(() => ResultType); 
        } 
    } 
    

    }

  3. 創建地圖實現

    mapper.CreateMap(); mapper.CreateMap(); mapper.AssertConfigurationIsValid();

    var test1 = new Model() {ResultType = "Test Result"}; 
        var s1 = mapper.Map<ViewModel>(test1); 
    

我得到AutoMapper映射的異常當我打電話mapper.Map。

缺少類型映射配置或不支持的映射。 映射類型: 型號 - >視圖模型 Support.Model - > Support.ViewModel

+0

請出示您的映射。 – Rabban

回答

1

提到的一樣,你實際上並沒有表現出任何的映射代碼。根據您所提供的是什麼,做這樣的事情:

var config = new MapperConfiguration(cfg => { 
    cfg.CreateMap<Model, ViewModel>(); 
}); 

這裏有一個工作示例:​​