问题描述:
AutoMapper 10.1.1 版本更新后,可能会出现 Mapper.CreateMap() 方法报错的问题。具体错误信息为:
System.InvalidOperationException: Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance.
解决办法:
这个问题的解决方法比较简单,只需要将 Mapper.CreateMap() 方法替换成新的方式即可。以下是更新后的代码示例:
旧版本的方式:
Mapper.CreateMap
新版本的方式:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap
其中,cfg 是新的 MapperConfiguration() 方法中的参数,CreateMapper() 方法则是创建一个新的 IMapper 实例,并包含了更新后的 CreateMap() 方法。这个解决方法的好处是,不需要手动调用 Initialize() 方法,才能正确运行代码。
上一篇:AutoMapper10.0或AutoMapper.Data5.0现在在AddDataReaderMapping调用时抛出InvalidOperationException。
下一篇:Automapper10和SimpleInjectorv5设置'SimpleInjector.ActivationException””