{"record":{"id":"497542a9a0ac262a","repo":"LuckyPennySoftware/AutoMapper","slug":"error-building-path-mapping-strategy","errorCode":null,"errorMessage":"Error building path mapping strategy.","messagePattern":"Error building path mapping strategy\\.","errorType":"exception","errorClass":"AutoMapperMappingException","httpStatus":null,"severity":"error","filePath":"src/AutoMapper/Execution/TypeMapPlanBuilder.cs","lineNumber":267,"sourceCode":"        if (pathMaps.Count == 0)\n        {\n            return;\n        }\n\n        foreach (var pathMap in pathMaps)\n        {\n            if (pathMap.Ignored)\n            {\n                continue;\n            }\n\n            try\n            {\n                actions.Add(TryPathMap(pathMap));\n            }\n            catch (Exception e) when (e is not AutoMapperConfigurationException)\n            {\n                throw new AutoMapperMappingException(\"Error building path mapping strategy.\", e, pathMap);\n            }\n        }\n    }\n\n    private void AddBeforeMap(List<Expression> actions)\n    {\n        var beforeMap = _typeMap.BeforeMapActions;\n        if (beforeMap.Count == 0)\n        {\n            return;\n        }\n\n        foreach (var beforeMapAction in beforeMap)\n        {\n            actions.Add(ReplaceParameters(beforeMapAction));\n        }\n    }\n","sourceCodeStart":249,"sourceCodeEnd":285,"githubUrl":"https://github.com/LuckyPennySoftware/AutoMapper/blob/dfa6dd587c5854b4beee5934beb39ba6e9569b84/src/AutoMapper/Execution/TypeMapPlanBuilder.cs#L249-L285","documentation":"Thrown during execution-plan compilation when building the expression for a ForPath mapping fails with a non-AutoMapperConfigurationException error. The AddPathMaps method catches the inner exception and rethrows it as an AutoMapperMappingException wrapping the PathMap context. This is a compile-time (configuration build) error, not a runtime mapping error.","triggerScenarios":"A ForPath configuration whose destination path or source expression produces an invalid expression tree during plan building — e.g., a type mismatch in an intermediate path segment, a null-path that cannot be constructed, or an incompatible MapFrom expression type.","commonSituations":"ForPath to a nested object where intermediate property types don't align; ForPath combined with custom ConvertUsing that returns incompatible types; ForPath where the source MapFrom lambda has a type mismatch.","solutions":["Inspect the InnerException of the AutoMapperMappingException for the root cause.","Verify that the ForPath destination expression and the MapFrom source expression produce compatible types at every path segment.","Simplify the ForPath configuration to isolate which segment fails, then re-add complexity.","Ensure intermediate objects in the ForPath path have settable properties or can be auto-constructed."],"exampleFix":"// before — type mismatch in ForPath\nCreateMap<Source, Dest>()\n    .ForPath(d => d.Address.ZipCode, opt => opt.MapFrom(s => s.ZipCodeString));\n// Address.ZipCode is int but s.ZipCodeString is string\n\n// after — map the correct type or add a converter\nCreateMap<Source, Dest>()\n    .ForPath(d => d.Address.ZipCode, opt => opt.MapFrom(s => int.Parse(s.ZipCodeString)));","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    var config = new MapperConfiguration(cfg => cfg.CreateMap<Source, Dest>()\n        .ForPath(d => d.Address.City, opt => opt.MapFrom(s => s.City)));\n    config.CompileMaps(); // triggers plan building for ForPath\n}\ncatch (AutoMapperMappingException ex) when (ex.Message.Contains(\"path mapping strategy\"))\n{\n    // ex.InnerException holds the root cause; inspect it for type mismatches\n    logger.LogError(ex.InnerException, \"ForPath plan build failed for {Types}\", ex.Types);\n}","preventionTips":["Ensure ForPath destination expression segments and MapFrom source expression produce compatible types.","Call CompileMaps() or AssertConfigurationIsValid() in unit tests to catch plan-building errors at test time.","Inspect the InnerException of AutoMapperMappingException for the root cause.","Ensure intermediate objects in ForPath paths have settable properties for auto-construction."],"tags":["execution","forpath","expression-tree","plan-building"],"backgroundTag":null,"analyzedSha":"dfa6dd587c5854b4beee5934beb39ba6e9569b84","analyzedAt":"2026-08-13T19:56:33.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}