.NET Core中AutoMapper 9.0配置失败:映射异常求助
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.Materialinstance to anIEnumerable<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:
- Confirm your DTO classes have properties matching the mapped types (e.g.,
TechnicalDrawingsshould beIEnumerable<int>if you’re mapping fromIdvalues). - 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

