报错:Missing type map configuration or unsupported mapping


报错:Missing type map configuration or unsupported mapping

□ 背景

当把View Model转换成Domain Model保存的时候,发生在AutoMapper的错误。

 

□ 分析

1、在派生于AutoMapper的Profile的类中已经建立映射:
Mapper.CreateMap<SomeDomainModel, SomeViewModel>();

 

2、也已经初始化派生于Profile的类:

    public static class AutoMapperConfiguration
    {
        public static void Configure()
        {
            Mapper.Initialize(x => x.AddProfile<SomeProfile>());
 
        }
    }

 

3、在全局中也注册了:

        protected void Application_Start()
        {
            //配置映射
            AutoMapperConfiguration.Configure();
        }    

 

4、单元测试也通过:

    [TestClass]
    public class AutoMapperConfigurationTester
    {
        [TestMethod]
        public void TestMethod1()
        {
            AutoMapperConfiguration.Configure();
            Mapper.AssertConfigurationIsValid();
        }
    }

□ 解决方法

在实际映射的时候,把AutoMapper.Mapper.Map<Source, Destination>换成AutoMapper.Mapper.DynamicMap<Source, Destination>

DomainModel someDomainModel = AutoMapper.Mapper.Map<ViewModel, DomainModel>(someViewModel);

改成:

DomainModel someDomainModel = AutoMapper.Mapper.DynamicMap<ViewModel, DomainModel>(someViewModel);    


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM