Dapper icon indicating copy to clipboard operation
Dapper copied to clipboard

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

Open Moulde opened this issue 9 years ago • 3 comments

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.

Moulde avatar Sep 27 '16 08:09 Moulde

Well that's a bug, agreed. Dapper should be ignoring them.

On 27 September 2016 at 09:30, Malte Baden Hansen [email protected] wrote:

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, Action2 paramReader) at Dapper.SqlMapper.<QueryImpl>d__1251.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable1 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.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/StackExchange/dapper-dot-net/issues/621, or mute the thread https://github.com/notifications/unsubscribe-auth/AABDsBvc7L9igBiEzLzWQVCw8vU1UjIdks5quNQKgaJpZM4KHY2M .

Regards,

Marc

mgravell avatar Sep 27 '16 08:09 mgravell

I am encountering this now in 2022 with the exact error message given in the original post, which is very confusing why this bug is still out there. Or at least it should be mentioned somewhere in the documentation that you can not put static properties in your model/DTO.

StepKie avatar Oct 15 '22 16:10 StepKie

2025 still there, but this page is first to be googled

delusionll avatar Jan 22 '25 13:01 delusionll