在 .Net Core 中配置 Automapper 9.0

Configure Automapper 9.0 in .Net Core

我遵循了官方文档,说明了关于 Automapper 9.0 从静态 API 的转变,但我仍然无法让它在我的 c# dotnet 核心应用程序中工作。我收到以下异常:

"AutoMapperMappingException: missing type map configuration or unsupported mapping" in my controller class."

你能告诉我关于 Automapper 9.0 的配置步骤吗?

这里是我执行的步骤:

Startup.cs

      var mappingConfig = new MapperConfiguration(mc =>
        {
           mc.AddProfile(new MyMapperProfile());
         });

   IMapper mapper = mappingConfig.CreateMapper();
   services.AddSingleton(mapper);

MyMapperProfile class

CreateMap<MasterData.Models.SupplierMaterial, SupplierMaterial>().
            ForMember(dto => dto.TechnicalDrawings, conf => conf.MapFrom(ol =>
                       ol.TechnicalDrawings.Select(v => v.Id)));

            CreateMap<MasterData.Models.WarehouseUbication, WarehouseUbication>().
            ForMember(dto => dto.MaterialCode, conf => conf.MapFrom(ol => ol.Material.Code));

            CreateMap<MasterData.Models.TechnicalDrawing, TechnicalDrawing>().
                ForMember(dto => dto.MaterialCode, conf => conf.MapFrom(ol => ol.Materials.Select(v => v.Code)))
               .ForMember(dto => dto.SupplierMaterialCode, conf => conf.MapFrom(ol => ol.SupplierMaterials.Select(v => v.Code)));

            CreateMap<MasterData.Models.MaterialDocument, MaterialDocument>().
                ForMember(dto => dto.Materials, conf => conf.MapFrom(ol => ol.Materials.Select(v => v.Code)));

            CreateMap<MasterData.Models.Material, Material>().
                ForMember(dto => dto.WarehouseUbications, conf => conf.MapFrom(ol => ol.WarehouseUbications.Select(v => v.Code)))
                .ForMember(dto => dto.SupplierMaterials, conf => conf.MapFrom(ol => ol.SupplierMaterials.Select(v => v.Code)))
                .ForMember(dto => dto.TechnicalDrawingID, conf => conf.MapFrom(ol => ol.TechnicalDrawing.Id))
                .ForMember(dto => dto.MaterialDocuments, conf => conf.MapFrom(ol => ol.MaterialDocuments.Select(v => v.Materials)));


            //var mapper = config.CreateMapper();

        }



    }
}

控制器class

public MaterialController(IMasterDataService masterService, IMapper mapper)
        {
            this.masterDataService = masterService;
            _mapper = mapper;
        }



  //[route]
     public List<Material> getAllMaterials()
      {
                List<Material> materialsList = new List<Material>();           

                foreach (MasterData.Models.Material m in masterDataService.GetMaterials())
                {
                    _mapper.Map<IEnumerable<Contracts.Material>>(m);
                }            

                return materialsList;
            }
public class MasterDataMapperProfile : Profile
{
    public MyMapperProfile()
    {
        CreateMap<MasterData.Models.SupplierMaterial, SupplierMaterial>().
            ForMember(dto => dto.TechnicalDrawings, conf => conf.MapFrom(ol => 
                       ol.TechnicalDrawings.Select(v => v.Id)));
    }
}

我认为这应该可以解决问题。目前在配置文件中,您正在创建一个新配置,在该配置上注册您的映射,然后丢弃该配置。

希望对您有所帮助

错误出在您的控制器代码中。您应该使用内置的集合映射支持直接从一种集合类型映射到另一种集合类型,或者单独映射每个元素,自己构建目标集合。目前,您只是丢弃映射的对象并返回一个空列表。

public List<Material> getAllMaterials()
{
   var materialsList = _mapper.Map<List<Contracts.Material>>(masterDataService.GetMaterials())

   return materialsList;
}

或:

public List<Material> getAllMaterials()
{
  List<Material> materialsList = new List<Material>();           

  foreach (MasterData.Models.Material m in masterDataService.GetMaterials())
  {
     var mapped = _mapper.Map<Contracts.Material>(m);
     materialsList.Add(mapped);
  }            

  return materialsList;
}

你的控制器代码应该是这样的

 public List<Material> getAllMaterials()
 {
            List<Material> materialsList = _mapper.Map<List<Contracts.Material>>(masterDataService.GetMaterials());

            return materialsList;
  }

否则,您就是将单个对象映射到列表。你也返回空列表。