DapperLib / DapperLib/Dapper

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

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

Nobody has claimed this yet.

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

Description

PS: Sorry for the VB.NET 😛

Imagine these classes:

Public Class EntityBase
  Public Property ColumnA As String
  Public Property ColumnB As String

  Public Class QueryEntity
    Inherits EntityBase

    Public Shadows Property ColumnA As DbString
    Public Shadows Property ColumnB As DbString
  End Class
End Class

The idea of this is to use EntityBase as the Model for the application, but be able to construct a QueryEntity whenever I need to Query/UPSERT data.
However, using these instance hierarchy like this ends up in:

System.Data.SqlClient.SqlException (0x80131904): The variable name '@ColumnA' has already been declared. Variable names must be unique within a query batch or stored procedure. Must declare the scalar variable "@ColumnB". 
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) 
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) 
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) 
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) 
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) 
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite) 
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) 
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) 
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() 
at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action`2 paramReader) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 3397 
at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1346 
at Dapper.SqlMapper.Execute(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable`1 commandTimeout, Nullable`1 commandType) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1221 
at ...

This may very well be a known-issue and leave it be, but thought it worth bringing it up.

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

Reproduce the issue with the nested VB.NET EntityBase and QueryEntity classes, then start at Dapper\SqlMapper.cs around lines 3397 and 1346 where execution and parameter handling appear in the trace. Trace how shadowed properties become SQL parameters; done means the hierarchy no longer produces duplicate variable declarations or missing scalar variables.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sql
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.