如何在AutoMapper 9.0中替换`AutoMapper.Mapper.Initialize`静态调用?

问题描述 投票:-1回答:1

在我的Startup.cs文件中,我的ConfigureServices方法中有一个AutoMapper配置:

AutoMapper.Mapper.Initialize(c => c.AddMaps(typeof(Models.MapperProfile), typeof(Data.Ef.MapperProfile)));
namespace Rm.Combo.Api.Models
{
    public class MapperProfile : Profile
    {
        public MapperProfile()
        {
            CreateMap<NewCashoutModel, App.Cashouts.InitiateCashoutCommand>();
        }
    }
}
namespace Rm.Combo.Data.Ef
{
    public class MapperProfile : Profile
    {
        public MapperProfile()
        {
            CreateMap<Domain.Cashouts.Cashout, Data.Cashouts.CashoutModel>();
        }
    }
}

自从我从版本8.1.1迁移到9.0.0以来,似乎有一些重大更改。

我试图检查那些特定的链接:

但是他们都没有说如何

c# asp.net-core .net-core automapper
1个回答
1
投票

从9.0开始,静态API不再可用。

您可以通过依赖注入使用AutoMapper,如下所示:

1。安装AutoMapper.Extensions.Microsoft.DependencyInjection

2。在Startup.cs的AutoMapper.Extensions.Microsoft.DependencyInjection中注册服务:

ConfigureServices

参考:services.AddAutoMapper(typeof(MapperProfile));

© www.soinside.com 2019 - 2024. All rights reserved.