{"record":{"id":"1f08d1d418c7733d","repo":"LuckyPennySoftware/AutoMapper","slug":"error-building-constructor-parameter-mapping-strat","errorCode":null,"errorMessage":"Error building constructor parameter mapping strategy.","messagePattern":"Error building constructor parameter mapping strategy\\.","errorType":"exception","errorClass":"AutoMapperMappingException","httpStatus":null,"severity":"error","filePath":"src/AutoMapper/Execution/TypeMapPlanBuilder.cs","lineNumber":395,"sourceCode":"            interfaceType),\n        _ => ObjectFactory.GenerateConstructorExpression(DestinationType, _configuration)\n    };\n\n    private Expression ConstructorMapping(ConstructorMap constructorMap)\n    {\n        List<ParameterExpression> variables = [];\n        List<Expression> body = [];\n        foreach (var parameter in constructorMap.CtorParams)\n        {\n            try\n            {\n                var variable = Variable(parameter.DestinationType, parameter.DestinationName);\n                variables.Add(variable);\n                body.Add(Assign(variable, CreateConstructorParameterExpression(parameter)));\n            }\n            catch (Exception e) when (e is not AutoMapperConfigurationException)\n            {\n                throw new AutoMapperMappingException(\"Error building constructor parameter mapping strategy.\", e, parameter);\n            }\n        }\n\n        body.Add(CheckReferencesCache(New(constructorMap.Ctor, variables)));\n        return Block(variables, body);\n    }\n\n    private Expression CreateConstructorParameterExpression(ConstructorParameterMap ctorParamMap)\n    {\n        var defaultValue = ctorParamMap.DefaultValue(_configuration);\n        var customSource = GetCustomSource(ctorParamMap);\n        var resolvedExpression = BuildValueResolverFunc(ctorParamMap, customSource, defaultValue);\n        var resolvedValue = Variable(resolvedExpression.Type, \"resolvedValue\");\n        var mapMember = MapMember(ctorParamMap, resolvedValue, defaultValue);\n        _variables.Clear();\n        _variables.Add(resolvedValue);\n        _expressions.Clear();\n        _expressions.Add(Assign(resolvedValue, resolvedExpression));","sourceCodeStart":377,"sourceCodeEnd":413,"githubUrl":"https://github.com/LuckyPennySoftware/AutoMapper/blob/dfa6dd587c5854b4beee5934beb39ba6e9569b84/src/AutoMapper/Execution/TypeMapPlanBuilder.cs#L377-L413","documentation":"Thrown during execution-plan compilation when building the expression for a constructor parameter mapping fails with a non-AutoMapperConfigurationException error. The ConstructorMapping method catches the inner exception and rethrows it as AutoMapperMappingException wrapping the ConstructorParameterMap context. This happens at configuration-build time when resolving how each constructor parameter gets its value.","triggerScenarios":"A ForCtorParam configuration or an auto-resolved constructor parameter whose source-to-destination type resolution produces an invalid expression tree — e.g., the ctor param type doesn't match the source member type and no converter is registered, or a MapFrom on the ctor param returns an incompatible type.","commonSituations":"Constructor parameter type mismatch (e.g., ctor param is int but source member is string); ForCtorParam MapFrom returning wrong type; missing type map for a complex ctor parameter.","solutions":["Inspect the InnerException of the AutoMapperMappingException for the root cause.","Verify that each constructor parameter's resolved type matches its declared type, or add a converter.","Use ForCtorParam with an explicit MapFrom lambda that returns the correct type.","If the parameter type is complex, register a CreateMap or ConvertUsing for it."],"exampleFix":"// before — ctor param type mismatch\npublic Dest(int id) { }\nCreateMap<Source, Dest>().ForCtorParam(\"id\", opt => opt.MapFrom(s => s.IdString));\n// s.IdString is string but ctor param is int\n\n// after — convert in MapFrom\nCreateMap<Source, Dest>().ForCtorParam(\"id\", opt => opt.MapFrom(s => int.Parse(s.IdString)));","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    var config = new MapperConfiguration(cfg => cfg.CreateMap<Source, Dest>()\n        .ForCtorParam(\"id\", opt => opt.MapFrom(s => s.Id)));\n    config.CompileMaps();\n}\ncatch (AutoMapperMappingException ex) when (ex.Message.Contains(\"constructor parameter mapping strategy\"))\n{\n    // ex.InnerException has the root cause\n    logger.LogError(ex.InnerException, \"Ctor param plan build failed.\");\n}","preventionTips":["Ensure each constructor parameter's resolved type matches its declared type or add a converter.","Use AssertConfigurationIsValid in tests to catch plan-building errors at test time.","Inspect the InnerException for the specific type mismatch.","Prefer lambda-based ForCtorParam MapFrom so the compiler checks types."],"tags":["execution","constructor","expression-tree","plan-building"],"backgroundTag":null,"analyzedSha":"dfa6dd587c5854b4beee5934beb39ba6e9569b84","analyzedAt":"2026-08-13T19:56:33.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}