LuckyPennySoftware/AutoMapper · error · AutoMapperMappingException

Error building constructor parameter mapping strategy.

Error message

Error building constructor parameter mapping strategy.

What it means

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.

Source

Thrown at src/AutoMapper/Execution/TypeMapPlanBuilder.cs:395

            interfaceType),
        _ => ObjectFactory.GenerateConstructorExpression(DestinationType, _configuration)
    };

    private Expression ConstructorMapping(ConstructorMap constructorMap)
    {
        List<ParameterExpression> variables = [];
        List<Expression> body = [];
        foreach (var parameter in constructorMap.CtorParams)
        {
            try
            {
                var variable = Variable(parameter.DestinationType, parameter.DestinationName);
                variables.Add(variable);
                body.Add(Assign(variable, CreateConstructorParameterExpression(parameter)));
            }
            catch (Exception e) when (e is not AutoMapperConfigurationException)
            {
                throw new AutoMapperMappingException("Error building constructor parameter mapping strategy.", e, parameter);
            }
        }

        body.Add(CheckReferencesCache(New(constructorMap.Ctor, variables)));
        return Block(variables, body);
    }

    private Expression CreateConstructorParameterExpression(ConstructorParameterMap ctorParamMap)
    {
        var defaultValue = ctorParamMap.DefaultValue(_configuration);
        var customSource = GetCustomSource(ctorParamMap);
        var resolvedExpression = BuildValueResolverFunc(ctorParamMap, customSource, defaultValue);
        var resolvedValue = Variable(resolvedExpression.Type, "resolvedValue");
        var mapMember = MapMember(ctorParamMap, resolvedValue, defaultValue);
        _variables.Clear();
        _variables.Add(resolvedValue);
        _expressions.Clear();
        _expressions.Add(Assign(resolvedValue, resolvedExpression));

View on GitHub (pinned to dfa6dd587c)

Solutions

  1. Inspect the InnerException of the AutoMapperMappingException for the root cause.
  2. Verify that each constructor parameter's resolved type matches its declared type, or add a converter.
  3. Use ForCtorParam with an explicit MapFrom lambda that returns the correct type.
  4. If the parameter type is complex, register a CreateMap or ConvertUsing for it.

Example fix

// before — ctor param type mismatch
public Dest(int id) { }
CreateMap<Source, Dest>().ForCtorParam("id", opt => opt.MapFrom(s => s.IdString));
// s.IdString is string but ctor param is int

// after — convert in MapFrom
CreateMap<Source, Dest>().ForCtorParam("id", opt => opt.MapFrom(s => int.Parse(s.IdString)));
Defensive patterns

Strategy: try-catch

Try / catch

try
{
    var config = new MapperConfiguration(cfg => cfg.CreateMap<Source, Dest>()
        .ForCtorParam("id", opt => opt.MapFrom(s => s.Id)));
    config.CompileMaps();
}
catch (AutoMapperMappingException ex) when (ex.Message.Contains("constructor parameter mapping strategy"))
{
    // ex.InnerException has the root cause
    logger.LogError(ex.InnerException, "Ctor param plan build failed.");
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of LuckyPennySoftware/AutoMapper@dfa6dd587c (2026-08-13). Data as JSON: /api/errors/1f08d1d418c7733d. Report an issue: GitHub.