2017-02-06 47 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>(); 
}); 

这里有一个工作示例:​​