DapperLib / DapperLib/Dapper

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

Open
#621 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug v3.0
Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the C# reproduction and the parameter-binding path shown in the stack trace, especially CommandDefinition.SetupCommand and SqlMapper.Query. Determine whether static properties should be accepted or rejected, then make the resulting failure explain the cause and verify it with a regression test for the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sql
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.