AutoMapper从静态API迁移 [英] AutoMapper Migrating from static API

查看:132
本文介绍了AutoMapper从静态API迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://github.com/AutoMapper/AutoMapper/wiki/Migrating-from-static -API

这一变化打破了我的系统

this change breaks my system.

更新之前,我使用的:

===> Startup.cs

===> Startup.cs

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
    ...
        MyAutoMapperConfiguration.Configure();
    }
}



===> MyAutoMapperConfiguration.cs

===> MyAutoMapperConfiguration.cs

public class MyAutoMapperConfiguration
{
    public static void Configure()
    {
        Mapper.Initialize(a =>
        {
            a.AddProfile<AbcMappingProfile>();
            a.AddProfile<XyzMappingProfile>();
            a.AddProfile<QweMappingProfile>();
        });
    }
}



===> AbcMappingProfile.cs

===> AbcMappingProfile.cs

public class AbcMappingProfile : Profile
{
    protected override void Configure()
    {
        Mapper.CreateMap<AbcEditViewModel, Abc>();
        Mapper.CreateMap<Abc, AbcEditViewModel>();
        ...
    }
}



ERROR:

ERROR:

Mapper.CreateMap()已过时:静态API将在5.0版本中移除。使用MapperConfiguration实例,并根据需要静态存储。使用CreateMapper创建一个映射instanace。

'Mapper.CreateMap()' is obsolete: 'The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instanace.'

我可以使用Mapper.Map。现在,我如何使用它

I can use Mapper.Map. Now How can I use it

推荐答案

而不是:

Mapper.CreateMap<AbcEditViewModel, Abc>();

新的语法是:

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



然后:

Then:

IMapper mapper = config.CreateMapper();
var source = new AbcEditViewModel();
var dest = mapper.Map<AbcEditViewModel, Abct>(source);



来源举出更多的例子

这篇关于AutoMapper从静态API迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆