-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for ExecuteScalar #9882
Comments
@danobri We have discussed this and decided that we will look into doing this. The main reason we haven't done this in the past is because ExecuteScalar is quite a confusing method. Consider this stored proc: CREATE PROCEDURE [dbo].[GetFoo]
AS
BEGIN
SELECT 76
RETURN 77
END There is often an expectation that calling ExecuteScalar for this would return 77, since this is the "return value" of the procedure. But it will actually return 76. This also illustrates that ExecuteScalar is really a form of sugar over ExecuteReader. For example, in your code above you could have done this instead: using (var reader = command.ExecuteReader())
{
reader.Read();
return reader.GetInt32(0);
} This means that once #1862 is implemented, then it would be possible to write: var value = context.Database.FromSql<int>("SomeStoredProc").First(); and have it return the same thing as an ExecuteScalar. This is possibly less ambiguous than ExecuteScalar and doesn't involve new API surface. All that being said, ExecuteScalar may perform better (not tested) and may help with discovery for people who know how ExecuteScalar works and are looking for it. So putting this on the backlog to consider in the future. |
So #1862 is implemented, how do I do ExecuteScalar now? |
Is this a duplicate of #11624? |
Triage: This is a sub-scenario of #11624; tracking there. |
Note from triage: We don't plan to support this directly. #11624 covers the common scenarios that might use this functionality, or the application can do an ExecuteScalar directly on the DbConnection. |
AFAIK the only way to call a stored procedure that returns a scalar value is to use the underlying ADO.Net API like this:
Alternatively, the stored procedure can be written to use an output parameter, and can then be called using the context.Database.ExecuteSqlCommand function, but that feels unnecessarily complicated, and requires existing procedures to be re-written.
Is there any reason the EF Core team has chosen not to include ExecuteScalar / ExecuteScalarAsync methods on the context.Database object?
The text was updated successfully, but these errors were encountered: