報錯: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