Skip to content

ExecuteScalar<T>: no rows into a non-nullable value type is an error, not default - #235

Merged
electricessence merged 1 commit into
masterfrom
feat/scalar-no-rows
Nov 24, 2026
Merged

electricessence merged 1 commit into
masterfrom
feat/scalar-no-rows

Conversation

@electricessence

Copy link
Copy Markdown
Collaborator

What

A typed scalar (ExecuteScalarAsync<T>(), ExecuteScalar<T>()) that returns no rows into a non-nullable value type now throws ValueMappingException instead of returning default(T):

The query returned no rows; ExecuteScalarAsync needs a value. Ask for Int32? and use ?? for a default.

No rows into a nullable or reference type is null. A NULL is unchanged. The Func<object?, T> overloads are unchanged. ExecuteReturn<T> is unchanged. There is no default-value overload: (await command.ExecuteScalarAsync<int?>()) ?? -1.

Why

default(T) for no rows made "no row" and a real 0 the same answer for an int. A caller who needs a value now finds out when there is none.

Behavior change

Code that read ExecuteScalarAsync<int>() and relied on 0 for no rows now throws. Ask for int?.

Tests and docs

Value, NULL and no-rows for int, long, DateTime, an enum, int?, Guid?, string and byte[], sync and async, with the exact message and no member, column or inner exception. The scalar section of the expressive commands page, the Dapper comparison line and the XML docs say the new rule.

🤖 Generated with Claude Code

… not default

A typed scalar that returned no rows used to give default(T): 0 for an int, indistinguishable from a real 0. Now:

- no rows into a non-nullable value type throws ValueMappingException: "The query returned no rows; ExecuteScalarAsync<Int32> needs a value. Ask for Int32? and use ?? for a default." (the sync methods name ExecuteScalar);
- no rows into a nullable or a reference type is null;
- a NULL is unchanged (null for a type that takes it, ValueMappingException otherwise);
- the Func<object?, T> overloads are unchanged: they get null for no rows.

There is no default-value overload: ask for int? and use ?? (`(await command.ExecuteScalarAsync<int?>()) ?? -1`).

Applies to ExecuteScalarAsync<T>(ct) and the sync ExecuteScalar<T>() on IExecuteCommand. ExecuteReturn<T> is not changed.

Tests: value, NULL and no rows for int, long, DateTime, an enum, int?, Guid?, string and byte[], sync and async, with the exact message, type, and no member, column or inner exception. Docs: the scalar section of the expressive commands page, the Dapper comparison line, and the XML docs.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@electricessence
electricessence marked this pull request as ready for review November 24, 2026 20:54
@electricessence
electricessence merged commit f3c7c99 into master Nov 24, 2026
10 checks passed
@electricessence
electricessence deleted the feat/scalar-no-rows branch November 24, 2026 20:58
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.

1 participant