Skip to content

Map object-typed columns to primitive members again (sql_variant to bool) - #2244

Open
Arthur031221 wants to merge 1 commit into
DapperLib:mainfrom
Arthur031221:fix/object-column-primitive-convert
Open

Arthur031221 wants to merge 1 commit into
DapperLib:mainfrom
Arthur031221:fix/object-column-primitive-convert

Conversation

@Arthur031221

Copy link
Copy Markdown

Columns that a provider reports as plain object (SQL Server sql_variant, e.g. SERVERPROPERTY('IsLocalDB')) failed to map when the boxed value didn't match the member type, like an Int32 box into a bool or bool?. The query threw Error parsing column 0 (IsLocalDB=0 - Int32) with Specified cast is not valid.

The DateOnly/TimeOnly change (#2228) added colType == typeof(object) to the direct unbox condition for every target type. I limited it to targets that are themselves TypeCode.Object (Guid, DateOnly, byte[] and so on). Primitive and string targets go back through the flexible convert path like in 2.1.79, where a plain bool member worked. Nullable ones such as bool? use it too.

The array mapping in #2231 and #2239 is separate and doesn't touch that clause. I added ObjectTypedColumnsConvertToMemberType and SqlVariantIntToBool to DataReaderTests. dotnet test tests/Dapper.Tests -f net10.0 passes against SQL Server 2022.

Fixes #2234

Assisted by Claude/Codex.

…ool)

A column reported as plain object keeps the direct unbox only when the target is itself TypeCode.Object. Primitive and string targets use the flexible convert path, as before the DateOnly/TimeOnly change.

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.

SQL Server sql_variant mapping regression in 2.1.86: SERVERPROPERTY Int32 to bool throws InvalidCastException

1 participant