Skip to content

Passing parameters object with static property causes dapper to fail with cryptic exception. #621

@Moulde

Description

@Moulde

Hi, i recently had an issue where Dapper would throw a very cryptic exception.

After debugging for a while i discovered that it was caused by a property on the object that we pass as parameters. The issue was that this property was a static property.

Here is a small example.

void Main()
{
    SqlConnection con = new SqlConnection("..ConnectionString..");
    con.Open();

    Parameters.StaticParameter = "Value from static parameter";
    var pars = new Parameters() { Parameter = "Value from parameter" };

    var result = con.Query<Row>("SELECT ValueColumn = @parameter, ValueColumn2 = @StaticParameter", pars);
}

public class Parameters
{
    public string Parameter { get; set; }
    public static string StaticParameter { get; set; }
}

public class Row
{
    public string ValueColumn { get; set; }
}

This causes the following exception to be thrown

MissingMethodException
Method not found: '?'. 

Stacktrade:
   at ParamInfofa1e955c-07bc-4f1a-a85a-8fd782a3b051(IDbCommand , Object )
   at Dapper.CommandDefinition.SetupCommand(IDbConnection cnn, Action`2 paramReader)
   at Dapper.SqlMapper.<QueryImpl>d__125`1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType)
   at UserQuery.Main()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Should dapper allow properties to be static? I don't know, but I feel the exception that it throws, could at least be a bit more detailed as to why it failed.

I may find the time to improve the exception thrown and submit a pull request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugv3.0Changes awaiting the next breaking release

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions