Skip to content

Prefer derived properties when binding shadowed parameters - #2282

Open
tuncaypeker wants to merge 1 commit into
DapperLib:mainfrom
tuncaypeker:fix/shadowed-parameter-properties
Open

tuncaypeker wants to merge 1 commit into
DapperLib:mainfrom
tuncaypeker:fix/shadowed-parameter-properties

Conversation

@tuncaypeker

Copy link
Copy Markdown
Contributor

Fixes #381.

When a parameter object hides an inherited property with a different type, reflection returns both properties. Dapper can then send duplicate parameter names or use the base value instead of the derived value. A base string property hidden by a DbString property reproduces the issue as SQL Server's "variable name has already been declared" error.

During parameter-reader generation, select the most-derived declaring type for each case-insensitive property name before constructor-order detection. Keep case-distinct properties declared at the same level. The selection runs only during generation; cached command binding remains unchanged. This follows the behavior proposed by mgravell in #381 (comment).

Validation:

  • Reproduced on unmodified upstream main b5e17f9 with SQL Server 2025 LocalDB and both Microsoft.Data.SqlClient and System.Data.SqlClient. The original minimal DbString example fails for both providers, while ordinary base-property controls pass.
  • An expanded initial set of 32 cases has 14 failures and 18 passes before the fix on net10.0. It covers direct and template binding, sync/async calls, DbString and ordinary scalar properties, case variations across hierarchy levels, three-level inheritance, unchanged inherited/same-type properties, same-level case-distinct properties, and constructor parameter order.
  • Related ParameterTests, LiteralTests, AsyncTests, SQLite tests, and the initial 32 new cases pass after the fix: net481 349 passed / 19 skipped; net8.0 and net10.0 each 344 passed / 16 skipped. Skips are existing disabled/performance/provider cases, including the known Microsoft.Data.SqlClient pipelining issue Async Pipelined execution does not work with Microsoft.Data.SqlClient. #2028.
  • The final 36 new cases, including the original minimal DbString reproduction and a literal-replacement regression, pass on net481, net8.0, and net10.0 with no failures or skips.
  • The full repository test suite was not run.
  • Release library build succeeds for net461, netstandard2.0, net8.0, and net10.0 with zero warnings or errors.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Variable Already Declared when Shadowed/New Properties exist in Hierarchy of Model

1 participant