C# 自动映射器中一种类型到另一种类型的泛型类

C# 自动映射器中一种类型到另一种类型的泛型类,c#,generics,asp.net-core,automapper,C#,Generics,Asp.net Core,Automapper,我有一个通用分页类:我想将PagedList映射到PagedList 公共类页面列表 { 公共页面列表() { } 公共页面列表(IList源、int pageNumber、int pageSize) { this.TotalItems=source.Count; this.PageNumber=页码; this.PageSize=页面大小; 这个项目=来源; } 公共整数TotalItems{get;set;} 公共整数页码{get;set;} 公共int PageSize{get;set;}

我有一个通用分页类:我想将
PagedList映射到PagedList

公共类页面列表
{
公共页面列表()
{
}
公共页面列表(IList源、int pageNumber、int pageSize)
{
this.TotalItems=source.Count;
this.PageNumber=页码;
this.PageSize=页面大小;
这个项目=来源;
}
公共整数TotalItems{get;set;}
公共整数页码{get;set;}
公共int PageSize{get;set;}
公共IEnumerable项{get;set;}
public int TotalPages=>(int)Math.天花(this.TotalItems/(double)this.PageSize);
}
以及模型和视图模型类

    public class Caste
    {
        public int Id { get; set; }
        public string CasteCode { get; set; }
        public string CasteDesc { get; set; }
        public bool IsActive { get; set; }
        public int? CasteParentId { get; set; }
        public virtual Caste CasteParent { get; set; }
        public virtual ICollection<Caste> CasteChildren { get; set; }

        public virtual ICollection<Customer> Customers { get; set; }

    }

public class CasteModel
    {
        public int Id { get; set; }
        public string CasteCode { get; set; }
        public string CasteDesc { get; set; }
        public bool IsActive { get; set; }
        public int? CasteParentId { get; set; }

    }
公共阶级种姓
{
公共int Id{get;set;}
公共字符串代码{get;set;}
公共字符串CasteDesc{get;set;}
公共bool IsActive{get;set;}
public int?CasteParentId{get;set;}
公共虚拟种姓种姓父项{get;set;}
公共虚拟ICollection子对象{get;set;}
公共虚拟ICollection客户{get;set;}
}
公共阶级模式
{
公共int Id{get;set;}
公共字符串代码{get;set;}
公共字符串CasteDesc{get;set;}
公共bool IsActive{get;set;}
public int?CasteParentId{get;set;}
}
下面是我的自动映射器配置

 public class AppProfile : Profile
    {
        public AppProfile()
        {

            //Masters
            CreateMap<CasteModel, Caste>();
            CreateMap<Caste, CasteModel>();

            CreateMap(typeof(PagedList<>), typeof(PagedList<>));
         // CreateMap<PagedList<Caste>, PagedList<CasteModel>>(); ---This also checked
        }
公共类AppProfile:Profile
{
公共应用程序配置文件()
{
//大师
CreateMap();
CreateMap();
CreateMap(typeof(PagedList),typeof(PagedList));
//CreateMap();---此选项也已选中
}
这是控制器中映射的代码

 PagedList<Caste> result = new PagedList<Caste>
                {
                     Items = new List<Caste> { new Caste { Id = 7, CasteCode="" } },
                     TotalItems = 1
                };

                var pagedListOfDtos = Mapper.Map<PagedList<CasteModel>>(result);
PagedList结果=新建页面列表
{
Items=新列表{new Caste{Id=7,CasteCode=”“},
TotalItems=1
};
var pagedlistoftos=Mapper.Map(结果);
当执行下面的错误时,我得到下面的异常

"映射器未初始化。请使用适当的配置调用Initialize。如果您试图通过容器或其他方式使用映射器实例,请确保您没有任何对静态映射器.Map方法的调用,如果您使用的是ProjectTo或UseAsDataSource扩展方法,请确保在中传递适当的IConfigurationProvider立场。”

Am使用Asp.net内核和automapper 6.1。代码基于以下链接编写 请建议一个me解决方案,尝试了很多次,但都得到了相同的消息

对于
Mapper.Map(结果);
,您需要初始化
Mapper
,如下所示
Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        Mapper.Initialize(cfg =>
        {
            cfg.AddProfile<AppProfile>();
        });
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }
public void ConfigureServices(IServiceCollection services)
{
    services.AddAutoMapper(typeof(Startup));
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
  • 用例

    public class ValuesController : ControllerBase
    {
        private readonly IMapper _mapper;
        public ValuesController(IMapper mapper)
        {
            _mapper = mapper;
        }
        // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            PagedList<Caste> result = new PagedList<Caste>
            {
                Items = new List<Caste> { new Caste { Id = 7, CasteCode = "" } },
                TotalItems = 1
            };
    
            var pagedListOfDtos = _mapper.Map<PagedList<CasteModel>>(result);
            return new string[] { "value1", "value2" };
        }       
    }
    
    公共类值控制器:控制器库
    {
    专用只读IMapper\u映射器;
    公共价值控制器(IMapper映射器)
    {
    _映射器=映射器;
    }
    //获取api/值
    [HttpGet]
    公共行动结果获取()
    {
    页面列表结果=新建页面列表
    {
    Items=新列表{new Caste{Id=7,CasteCode=”“},
    TotalItems=1
    };
    var pagedlistoftos=\u mapper.Map(结果);
    返回新字符串[]{“value1”,“value2”};
    }       
    }
    

  • Mapper是静态类,请尝试Mapper.CreateMap();pagingModel是Different模型..现在我已删除..我已尝试CreateMap();…但同样的问题只在泛型类映射中出现,即PagedList到PagedList。有可能将一种类型的泛型类映射到另一种类型吗?或者有其他解决方案吗?您似乎没有初始化AutoMapper,这就是为什么会出现这样的错误:您有一个配置文件,但您必须告诉AutoMapper使用它!谢谢国王现在…但是Mapper.Initialize(cfg=>{cfg.AddProfile();});显示过时..有什么解决方案吗?@Ljt尝试使用依赖项InAction的第一个选项。是的..我正在使用依赖项注入的第一个选项..但是它现在可以工作..但是它在初始化的启动类中显示过时..任何方式谢谢reply@Ljt这是我的错误,请使用
    services.AddAutoMapper(typeof(Startup))尝试第二个选项;
    如果我只使用services.AddAutoMapper(typeof(Startup));在启动类中,我在问题中遇到了相同的错误,仅针对泛型类模型。对于其他类模型,它工作正常。可能是automapper问题
    public class ValuesController : ControllerBase
    {
        private readonly IMapper _mapper;
        public ValuesController(IMapper mapper)
        {
            _mapper = mapper;
        }
        // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            PagedList<Caste> result = new PagedList<Caste>
            {
                Items = new List<Caste> { new Caste { Id = 7, CasteCode = "" } },
                TotalItems = 1
            };
    
            var pagedListOfDtos = _mapper.Map<PagedList<CasteModel>>(result);
            return new string[] { "value1", "value2" };
        }       
    }