You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core中AutoMapper 9.0配置失败:映射异常求助

Fixing AutoMapper 9.0 "Missing Type Map Configuration" Exception in .NET Core

Let's break down what's going wrong in your setup and walk through the correct configuration steps to get AutoMapper working properly.

First, Identify the Critical Issues in Your Current Code

1. Incorrect Mapping Usage in the Controller

Looking at your getAllMaterials method, you're making two key mistakes:

  • You’re trying to map a single MasterData.Models.Material instance to an IEnumerable<Contracts.Material>—AutoMapper can’t resolve this mismatched request.
  • You’re not capturing the mapped result or adding it to your materialsList, so your method will always return an empty list.

2. Suboptimal Startup Configuration

While your manual Mapper registration technically works, AutoMapper provides an official .NET Core extension that simplifies setup and ensures proper dependency injection handling.

3. Potential Null Reference Risks

Some of your Select operations might fail if the source collection is null (e.g., ol.TechnicalDrawings could be null on a SupplierMaterial). Adding null checks will prevent unexpected crashes.


Correct AutoMapper 9.0 Configuration Steps

Step 1: Install the Required Package

Make sure you have the AutoMapper DI extension installed (this is essential for .NET Core integration):

Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection -Version 9.0.0

Step 2: Update Startup.cs Configuration

Replace your manual mapper registration with the official extension method. This automatically discovers your mapping profiles and registers IMapper correctly:

using AutoMapper;

public void ConfigureServices(IServiceCollection services)
{
    // Other service registrations...
    
    // Register AutoMapper and scan for profiles in the current assembly
    services.AddAutoMapper(typeof(MyMapperProfile));
}

This replaces your old manual MapperConfiguration setup—it’s cleaner and aligns with AutoMapper’s official .NET Core guidance.

Step 3: Fix Your Mapping Profile (Add Null Checks)

Update your MyMapperProfile to handle null collections safely using ?.Select():

using AutoMapper;

public class MyMapperProfile : Profile
{
    public MyMapperProfile()
    {
        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)));
    }
}

Step 4: Fix the Controller Mapping Logic

Update your getAllMaterials method to correctly map each instance and populate the result list. You can even simplify it with LINQ:

public List<Material> getAllMaterials()
{
    var sourceMaterials = masterDataService.GetMaterials();
    // AutoMapper handles collection mapping directly
    return _mapper.Map<List<Material>>(sourceMaterials);
}

If you prefer the loop approach for clarity:

public List<Material> getAllMaterials()
{
    List<Material> materialsList = new List<Material>();
    foreach (MasterData.Models.Material m in masterDataService.GetMaterials())
    {
        var mappedMaterial = _mapper.Map<Material>(m);
        materialsList.Add(mappedMaterial);
    }
    return materialsList;
}

Verify the Fix

After making these changes:

  1. Confirm your DTO classes have properties matching the mapped types (e.g., TechnicalDrawings should be IEnumerable<int> if you’re mapping from Id values).
  2. Run your application—you should no longer see the "missing type map configuration" exception, and the mapped DTO list will return as expected.

内容的提问来源于stack exchange,提问作者OuterSpace

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 22:07:41