QuerySingleOrDefault with SQLite in gives the error “'Must add values for the following parameters”
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 18.4k
- Forks
- 3.7k
- Avg merge
- 5h 8m
- Merged PRs (30d)
- 1
Description
I am trying to execute the following Dapper query against a SQLite 3 database:
conn.QuerySingleOrDefault<DalTableConfig>
("select id, modelId, tableName, schemaName
from tables where tableName = $tableName;", new {tableName});
yet executing this line gives me the error:
System.InvalidOperationException: 'Must add values for the following parameters: $tableName'
I have found a workaround that makes my query work, which is:
var parameters = new DynamicParameters();
parameters.Add("@tableName" , tableName);
conn.QuerySingleOrDefault<DalTableConfig>("select id, modelId, tableName, schemaName from tables where tableName = $tableName;", parameters);
I have debugged into the Dapper source and come across where the problem seems to arise. The exception is thrown on the second of these lines from the SqlMapper class's QuerySingleOrDefault method:
var command = new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None);
return QueryRowImpl<T>(cnn, Row.SingleOrDefault, ref command, typeof(T));
Here param contains the correct tableName property, and command has this in its Parameters collection. When I examine the QueryRowImpl method I find this code:
private static T QueryRowImpl<T>(IDbConnection cnn, Row row, ref CommandDefinition command, Type effectiveType)
{
object param = command.Parameters;
var identity = new Identity(command.CommandText, command.CommandType, cnn, effectiveType, param?.GetType());
var info = GetCacheInfo(identity, param, command.AddToCache);
IDbCommand cmd = null;
IDataReader reader = null;
bool wasClosed = cnn.State == ConnectionState.Closed;
try
{
cmd = command.SetupCommand(cnn, info.ParamReader);
if (wasClosed) cnn.Open();
reader = ExecuteReaderWithFlagsFallback(cmd, wasClosed, (row & Row.Single) != 0
? CommandBehavior.SequentialAccess | CommandBehavior.SingleResult // need to allow multiple rows, to check fail condition
: CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow);
This fails on the below line. It is this method that throws the exception:
ExecuteReaderWithFlagsFallback
In the above code, the CommandDefinition command argument still has a valid Parameters collection, but the local variable IDbCommand cmd comes out of SetupCommand with, strangely enough, two empty Parameters collections. So there seems to be some fault with SetupCommand or ParamReader that causes the parameters to be lost
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the SQLite reproduction in QuerySingleOrDefault and trace QueryRowImpl through CommandDefinition.SetupCommand, ParamReader, and ExecuteReaderWithFlagsFallback. Compare the anonymous-object parameter path with the DynamicParameters workaround and inspect the resulting IDbCommand Parameters collection. Done means the reported query binds $tableName correctly and the regression is covered by an appropriate test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, sqlite
- Domain
- database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100