【问题标题】:mapping my Object1 to Object2 got Unsupported mapping Error将我的 Object1 映射到 Object2 出现不支持的映射错误
【发布时间】:2022-11-29 14:34:15
【问题描述】:

我一直在寻找我编写的代码的答案,但我找不到任何解决方案以及它给我这个错误的原因。

我正在尝试将我的 Object1 映射到 Object2,但出现此错误,不确定我在代码中遗漏了什么。

Object1 obj1 = this._mapper.Map<Object1>(new Object2());

我也尝试过这种不同的方法,但仍然出错

//var obj1  = _mapper.Map<Object1>(request.Object2);

错误:

type": "https://httpstatuses.com/500",
  "title": "Internal Server Error",
  "status": 500,
  "detail": "Missing type map configuration or unsupported mapping.\r\n\r\nMapping types:\r\nObject -> Object1 ...... -> ......",

【问题讨论】:

标签: c# mapping


【解决方案1】:
  1. 创建一个新的配置文件,
    public class MappingProfile: Profile {
       public MappingProfile() {
          CreateMap<Object1, Object2>();
       }
     }
    
    1. 在您的Startup.cs中添加映射配置文件,
     public void ConfigureServices(IServiceCollection services) {
    
        // Auto Mapper Configurations
         var mapperConfig = new MapperConfiguration(mc =>
         {
             mc.AddProfile(new MappingProfile());
         });
    
         IMapper mapper = mapperConfig.CreateMapper();
         services.AddSingleton(mapper);
    
     }
    
    1. 将映射器注入控制器,
    private readonly IMapper _mapper;
    
    // For dependency injection
    public YourController(IMapper mapper) {
        _mapper = mapper;
    }
    
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 2020-04-16
    • 2021-09-17
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多