Skip to content

Querying int columns as strings has broken in 2.1.37 #2056

Closed
@sellotape

Description

@sellotape

Might be intentional, but worked in 2.1.35, and I suspect a lot of people will be depending on it and pretty surprised, as there's no mention of a breaking change in the release notes.

To replicate, simply (I'm using Azure SQL Server):

using IDbConnection cx = new SqlConnection("your-connection-string");
string? actual = await cx.QueryFirstOrDefaultAsync<string>("select id from someTableWithAnIntIdColumn").ConfigureAwait(false);

In 2.1.35, this returns e.g. "42". In 2.1.37 it throws System.InvalidCastException : Unable to cast object of type 'System.Int32' to type 'System.String'.

Fuller exception:

System.InvalidCastException : Unable to cast object of type 'System.Int32' to type 'System.String'.
   at Microsoft.Data.SqlClient.SqlDataReader.GetFieldValueFromSqlBufferInternal[T](SqlBuffer data, _SqlMetaData metaData, Boolean isAsync)
   at Microsoft.Data.SqlClient.SqlDataReader.GetFieldValueInternal[T](Int32 i, Boolean isAsync)
   at Microsoft.Data.SqlClient.SqlDataReader.GetFieldValue[T](Int32 i)
   at Dapper.SqlMapper.<>c__DisplayClass209_0`1.<UnderlyingReadViaGetFieldValueFactory>b__0(DbDataReader reader) in /_/Dapper/SqlMapper.cs:line 3134
   at Dapper.SqlMapper.QueryRowAsync[T](IDbConnection cnn, Row row, Type effectiveType, CommandDefinition command) in /_/Dapper/SqlMapper.Async.cs:line 496
   at <my test code>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions