DapperLib/Dapper · error · ArgumentException

Constructor parameter not found for {name}

Error message

Constructor parameter not found for {name}

What it means

DefaultTypeMap.GetConstructorParameter throws ArgumentException("Constructor parameter not found for {name}") when none of a constructor's parameters match the supplied columnName (via MatchFirstOrDefault over parameter names). This fires during constructor-based materialization when Dapper selected a constructor but the reader's column name has no matching parameter name (case-insensitive).

Source

Thrown at Dapper/DefaultTypeMap.cs:139

            {
                return withAttr[0];
            }

            return null;
        }

        /// <summary>
        /// Gets mapping for constructor parameter
        /// </summary>
        /// <param name="constructor">Constructor to resolve</param>
        /// <param name="columnName">DataReader column name</param>
        /// <returns>Mapping implementation</returns>
        public SqlMapper.IMemberMap GetConstructorParameter(ConstructorInfo constructor, string columnName)
        {
            var param = MatchFirstOrDefault(constructor.GetParameters(), columnName, static p => p.Name) ?? Throw(columnName);
            return new SimpleMemberMap(columnName, param);

            static ParameterInfo Throw(string name) => throw new ArgumentException("Constructor parameter not found for " + name);
        }

        /// <summary>
        /// Gets member mapping for column
        /// </summary>
        /// <param name="columnName">DataReader column name</param>
        /// <returns>Mapping implementation</returns>
        public SqlMapper.IMemberMap? GetMember(string columnName)
        {
            var property = MatchFirstOrDefault(Properties, columnName, static p => p.Name);

            if (property is not null)
                return new SimpleMemberMap(columnName, property);

            // roslyn automatically implemented properties, in particular for get-only properties: <{Name}>k__BackingField;
            var backingFieldName = "<" + columnName + ">k__BackingField";

            // preference order is:

View on GitHub (pinned to 72a54c475f)

Solutions

  1. Align the SQL column alias with the constructor parameter name (e.g. SELECT id AS UserId when the parameter is userId).
  2. Provide a parameterless constructor plus settable properties, or add an explicit constructor whose parameters all map to query columns.
  3. Use a CustomPropertyTypeMap or DefaultTypeMap variant that tolerates missing columns if appropriate.

Example fix

-- before
SELECT user_id, name FROM Users
// ctor: public User(int userId, string name)

-- after
SELECT user_id AS userId, name FROM Users
Defensive patterns

Strategy: validation

Validate before calling

// before querying, confirm each constructor parameter has a matching column alias
var ctorParams = typeof(T).GetConstructors().SelectMany(c => c.GetParameters()).Select(p => p.Name);
// ensure your SELECT list includes a column name matching each (case-insensitive)

Try / catch

try { var data = cnn.Query<T>(sql); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Constructor parameter not found"))
{ /* align column alias with the constructor parameter name */ }

Prevention

When it happens

Trigger: Materializing into a type whose only constructor has parameter names that do not align with the query's column names; a SQL column was renamed/aliased so it no longer matches the constructor parameter; selecting a type whose constructor has parameters without matching columns.

Common situations: Changing a stored procedure or SELECT column list without updating the DTO constructor; using column aliases (e.g. "user_id") that differ from constructor parameter names (e.g. "id"); constructor injection where one parameter has no database equivalent.

Related errors


AI-assisted analysis of DapperLib/Dapper@72a54c475f (2026-08-13). Data as JSON: /api/errors/7e0fc77d7409fa83. Report an issue: GitHub.