Skip to content

Fixes QueryMultiple corrupting a later Query<T>'s DynamicParameters -- fixes #243, #2091 - #2214

Merged
mgravell merged 1 commit into
DapperLib:mainfrom
NadeemAfana:bug/243_and_2091
Nov 20, 2026
Merged

mgravell merged 1 commit into
DapperLib:mainfrom
NadeemAfana:bug/243_and_2091

Conversation

@NadeemAfana

Copy link
Copy Markdown
Contributor

Summary

Calling a query (stored proc or text) via QueryMultiple(...).Read<T>() and then via Query<T>(...)/QueryAsync<T>(...) with DynamicParameters on the same SQL can cause the second call to send the wrong parameters, typically dropping them entirely. Depending on the target's signature this surfaces as:

It's intermittent in real apps (depends on which call populates the shared cache entry first) and clears on app restart because the corruption lives in the process-wide query cache.

Fixes #243. Fixes #2091.

Root cause

Query<T> and the first grid of a QueryMultiple over the same SQL share a single cache entry. A grid read only needs the deserializer and has no parameter instance, so it populates that shared entry with a null parameter
object. For DynamicParameters (whose real parameters live in an internal collection, not in the type's properties), Dapper then builds a parameter-reader from the type's properties (ParameterNames, RemoveUnused) that never emits the actual parameters. Because the grid identity still carries the parameter type, it collides with the Query<T> identity, so a later Query<T>
with DynamicParameters reuses that wrong reader and sends the wrong parameters (or none). Whichever call populates the entry first wins, which is why it's intermittent and clears on restart.

Anonymous/POCO parameters are unaffected since their parameters are their properties.

Fix

Identity.ForGrid(...) now passes null for parametersType (grid reads never bind parameters).
This:

  1. de-collides grid identities from Query<T> identities, so a Query<T> always builds/uses its own correct ParamReader; and
  2. makes GetCacheInfo skip building a ParamReader for grid reads.

Impact / trade-offs

  • Parameter binding is unaffected for normal queries; the command-level identity still carries parametersType.
  • The grid-0 deserializer for (sql, T) is no longer shared with a Query<T> for the same (sql, T) so at most one extra deserializer compile (one-time, cached). No per-call overhead.
    Conversely, deserializers are now shared across different parameter types, which the old keying prevented (a small net win).
  • A grid read can still share an entry with a parameterless Query<T>(sql) (both have parametersType == null); that's harmless as neither builds a ParamReader.

@mgravell

Copy link
Copy Markdown
Member

Verified on top of current main (alongside the GetCacheInfo hardening that just landed — the two fix the same poisoning from opposite ends, so both are in) and merging. Grid identities never bind parameters, so dropping the parameters type from them is exactly right, and this finally puts #243 and #2091 to rest. Thanks for the patience on a five-month-old PR!

@mgravell
mgravell merged commit a11a9b8 into DapperLib:main Nov 20, 2026
1 check passed
This was referenced Nov 20, 2026
mvmorten added a commit to mvmorten/dashboard-starter that referenced this pull request Nov 23, 2026
Updated [Dapper](https://github.com/DapperLib/Dapper) from 2.1.89 to
2.1.100.

<details>
<summary>Release notes</summary>

_Sourced from [Dapper's
releases](https://github.com/DapperLib/Dapper/releases)._

## 2.1.100

## What's Changed
* Fix 2.1.86 unbox-gate regressions: array columns to interface members,
sql_variant to primitives by @​mgravell in
DapperLib/Dapper#2271
* Fix command disposal, parameter callbacks, handler-cache staleness,
static properties, and type-map races by @​mgravell in
DapperLib/Dapper#2272
* Fix deserializer cache race by @​jnm2 in
DapperLib/Dapper#2219
* Fix cache corruption due to CollectCacheGarbage call during
GridReader/Async queries by @​aklein53 in
DapperLib/Dapper#2218
* Fix false cache hit accounting on initial MultiMapAsync execution by
@​Nacl-HCl in DapperLib/Dapper#2242
* Fixes QueryMultiple corrupting a later Query<T>'s DynamicParameters --
fixes #​243, #​2091 by @​NadeemAfana in
DapperLib/Dapper#2214
* Don't cancel the command after QueryUnbufferedAsync completes normally
by @​aaronwilliams-tesouro in
DapperLib/Dapper#2245
* Preserve Size set by type handlers for string parameters by
@​JiuYue0820 in DapperLib/Dapper#2221
* Fix MultiMapAsync disposing the reader before unbuffered results are
enumerated by @​jinseojang0903 in
DapperLib/Dapper#2229
* Stop boxing values read via GetFieldValue<T> for the error message by
@​Giorgi in DapperLib/Dapper#2265
* Fix list initializer in bulk Execute README example by @​dajiaohuang
in DapperLib/Dapper#2243

## New Contributors
* @​aklein53 made their first contribution in
DapperLib/Dapper#2218
* @​Nacl-HCl made their first contribution in
DapperLib/Dapper#2242
* @​NadeemAfana made their first contribution in
DapperLib/Dapper#2214
* @​aaronwilliams-tesouro made their first contribution in
DapperLib/Dapper#2245
* @​JiuYue0820 made their first contribution in
DapperLib/Dapper#2221
* @​jinseojang0903 made their first contribution in
DapperLib/Dapper#2229
* @​dajiaohuang made their first contribution in
DapperLib/Dapper#2243

**Full Changelog**:
DapperLib/Dapper@2.1.89...2.1.100

Commits viewable in [compare
view](DapperLib/Dapper@2.1.89...2.1.100).
</details>

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=Dapper&package-manager=nuget&previous-version=2.1.89&new-version=2.1.100)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore <dependency name> major version` will close this
group update PR and stop Dependabot creating any more for the specific
dependency's major version (unless you unignore this specific
dependency's major version or upgrade to it yourself)
- `@dependabot ignore <dependency name> minor version` will close this
group update PR and stop Dependabot creating any more for the specific
dependency's minor version (unless you unignore this specific
dependency's minor version or upgrade to it yourself)
- `@dependabot ignore <dependency name>` will close this group update PR
and stop Dependabot creating any more for the specific dependency
(unless you unignore this specific dependency or upgrade to it yourself)
- `@dependabot unignore <dependency name>` will remove all of the ignore
conditions of the specified dependency
- `@dependabot unignore <dependency name> <ignore condition>` will
remove the ignore condition of the specified dependency and ignore
conditions


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Michael Mortensen <mvmorten@users.noreply.github.com>
This was referenced Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants