Mapper不包含初始化AutoMapper C#的定义

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

我在asp.net C#项目中实现了Auto Mapper,但出现错误:

映射器不包含初始化的定义

我已经尝试过此示例Link Here

我粘贴了我的代码:

namespace NewsWeb.Web.Infrastructure
{
    public class AutomapperWebProfile:Profile
    {
        public AutomapperWebProfile()
        {
            CreateMap<DistrictDTO, District>().ReverseMap();
        }

        public static void Run()
        {
            Mapper.Initialize(a =>
            {
                a.AddProfile<AutomapperWebProfile>();
            });
        }
    }
}

在我的Golbal.asax.cs文件中:

 AutomapperWebProfile.Run();

错误图片:enter image description here

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

方法Mapper.Initialize从版本v9.0.0(doc)开始已过时,您需要改用MapperConfigurationdoc)。

var config = new MapperConfiguration(cfg => {
    cfg.AddProfile<AutomapperWebProfile>();
});

var mapper = config.CreateMapper();
// or
var mapper = new Mapper(config);
© www.soinside.com 2019 - 2024. All rights reserved.