Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Dapper cannot find a constructor that fits the query columns

ErrorA parameterless default constructor or one matching signature

Short answer

Dapper looks for a public parameterless constructor first. If your type has none, it needs one constructor whose parameter count, names, order and types equal the query columns. Add a parameterless constructor, or make the select list match the constructor exactly, including column order.

  • Applies to Dapper 2.1, .NET 8, MySqlConnector 2.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Write down the column names and order your SQL returns, then compare them with the constructor parameters of the target type.

    SELECT id, name, created_at FROM users LIMIT 1;
  2. 2Match what you see, then jump to its fix

    1. 1The target is a record or a class with one constructor that takes argumentsThe type has parameters only, and the columns do not line up with themFix
    2. 2Columns like user_id against a parameter named userIdColumn names use underscores and parameters do notFix
    3. 3Names and order are right, but the database returns another numeric or date type than the parameterConstructor type and column type differFix

What does the error look like?

  • A parameterless default constructor or one matching signature
  • The error appears when Dapper maps rows into a record or a class whose only constructor takes arguments

Cause 1 of 3

The type has parameters only, and the columns do not line up with them

1Check

In Dapper's DefaultTypeMap source, a constructor is skipped when its parameter count differs from the column count. Parameter names are compared with column names by position, ignoring case, and the types must be compatible.

grep -rn "record \|public .*(.* .*)" --include=*.cs Models/

2Fix

Select exactly the columns the constructor takes, in the same order and with the same names. Alias columns when the names differ.

public record User(int Id, string Name);

var users = conn.Query<User>("SELECT id AS Id, name AS Name FROM users");

3Verify

The query returns objects instead of throwing.

Console.WriteLine(users.First());

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

Column names use underscores and parameters do not

1Check

The same source has a static MatchNamesWithUnderscores flag, false by default. When it is true, underscores are ignored in the name comparison.

grep -rn "MatchNamesWithUnderscores" --include=*.cs .

2Fix

Turn the flag on once at startup, or alias the columns in SQL.

Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;

3Verify

A query with snake_case columns now maps.

var rows = conn.Query<User>("SELECT user_id, user_name FROM users");

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

Constructor type and column type differ

1Check

The source accepts a column when its type equals the parameter type (after unwrapping Nullable), or when a type handler, enum or char-from-string rule applies. A decimal column against an int parameter is not accepted.

DESCRIBE users;

2Fix

Change the parameter type to the type the database returns, cast in SQL, or register a type handler. A public parameterless constructor is accepted as soon as Dapper reaches it, so adding one is the simplest escape.

public class User
{
    public User() { }
    public int Id { get; set; }
    public string Name { get; set; } = "";
}

3Verify

The query returns objects. Check that the properties hold real values, not defaults.

Console.WriteLine(users.First().Name);

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Name every column in the select list, in the order of the constructor, instead of SELECT *.
  • Alias database columns to the property names once, in one place.
  • Add a public parameterless constructor to types that Dapper fills.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.