Repository navigation
ExecuteScalar<T>: no rows into a non-nullable value type is an error, not default - #235
Merged
Merged
Conversation
… 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
A typed scalar (
ExecuteScalarAsync<T>(),ExecuteScalar<T>()) that returns no rows into a non-nullable value type now throwsValueMappingExceptioninstead of returningdefault(T):No rows into a nullable or reference type is
null. ANULLis unchanged. TheFunc<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 real0the same answer for anint. A caller who needs a value now finds out when there is none.Behavior change
Code that read
ExecuteScalarAsync<int>()and relied on0for no rows now throws. Ask forint?.Tests and docs
Value, NULL and no-rows for
int,long,DateTime, an enum,int?,Guid?,stringandbyte[], 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