DapperLib / DapperLib/Dapper

TypeHandler not working correctly with DynamicParameters

Open
#275 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area:typehandlers
Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

It appears that when executing a stored procedure and feeding parameters in through a DynamicParameters object, the command will fail if one of the parameters is set to a type that has a TypeHandler set on SqlMapper.

It appears that Dapper never makes a check to establish whether there is an associated TypeHandler for the parameter (no calls against the Dictionary happen). The error is: No mapping exists from object type [ObjectType] to a known managed provider native type. Relevant stack trace is as follows:

   at System.Data.SqlClient.MetaType.GetMetaTypeFromValue(Type dataType, Object value, Boolean inferLen, Boolean streamAllowed)  
   at System.Data.SqlClient.SqlParameter.GetMetaTypeOnly()
   at System.Data.SqlClient.SqlParameter.get_DbType()
   at Dapper.DynamicParameters.AddParameters(IDbCommand command, Identity identity) in [..]\Dapper.cs:line 4677
   at Dapper.DynamicParameters.Dapper.SqlMapper.IDynamicParameters.AddParameters(IDbCommand command, Identity identity) in [..]\Dapper.cs:line 4569
   at Dapper.SqlMapper.<>c__DisplayClass52.<GetCacheInfo>b__4d(IDbCommand cmd, Object obj) in [..]\Dapper.cs:line 2091
   at Dapper.CommandDefinition.SetupCommand(IDbConnection cnn, Action2 paramReader) in [..]\Dapper.cs:line 177
   at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action2 paramReader) in [..]\Dapper.cs:line 3313
   at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) in [..]\Dapper.cs:line 1309
   at Dapper.SqlMapper.Execute(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable1 commandTimeout, Nullable1 commandType) in [..]\Dapper.cs:line 1183 

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 in Dapper.cs at DynamicParameters.AddParameters, around line 4677, and trace how parameters are added for a stored procedure. Reproduce the failure with a DynamicParameters value whose type has a SqlMapper TypeHandler, then verify that command setup succeeds without the provider type-mapping error.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.