-
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
SqlQuery doesn't support scalar values but works with unmapped types #30447
Comments
Have you tried as a workaround to add column name EF is expecting in your query, e.g. |
That works, but as you wrote, this is a workaround. It should work without |
This behavior is by design. As the docs explain, when you compose LINQ operators over your SQL query, EF must know what the column name is that's coming out of it - your SQL query is integrated as a subquery in the larger query. In your code above, you're composing FirstOrDefault on top of the SqlQuery, so it's your responsibility to add the |
Why is "as value" not needed in this example?
|
@goenning because no operators are being composed on top of SqlQuery. ToList simply loads all of the query results, whereas FirstOrDefault composes a |
@goenning I guess the DbReader just uses the first column no matter what name it has. While theSQL builder needs to know the columns name. So for scalars just assumes @roji why not just use * for scalars? Would it mean a too expensive internal implementation / changes for this single usecase? I'm excited in the design decision. Not parsing the actual name from the query I totally agree - not worth the effort. |
Star (*) might work in this very particular case, but depending what exactly you compose, it might not. Star in SQL can mean multiple columns, which wouldn't work in various other SQL contexts - in this scenario there's only ever a single column coming out etc. We generally avoid star because when reading results back, we'd have to read them by name (since ordering isn't known/clear); and that's slightly less efficient than reading by index. |
@roji Yeah sure, totally agree! Bin since you can read (the not sub selected) scalar value query with every column name, I guess in this case you say: just the first / single column. And so the SQL generator could say: scalar? Star (*) is just fine, since there can only be a single value. But yeah, following sub querys might fail. So I guess you just decided: call it value and the reader just takes the first column? |
Hmm... I think that we need a proper scalar function that will return the first column of the first row in the result set returned by the query, just like https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.executescalar?view=dotnet-plat-ext-8.0 I use this workaround:
but as I wrote this is a workaround and with this, I must change my query from to: |
And it seems like SELECT t."Value"
FROM (
SELECT id as Value FROM app_environments WHERE app_key = @p0
) AS t Npgsql.PostgresException (0x80004005): 42703: column t.Value does not exist And I'm forced to wrap it on double quotes :( |
Yep, you indeed need the double-quotes since PG folds unquoted identifiers to lowercase. |
We discussed this and we're happy with the current design. While we could have special cases to handle some queries that are composed over by LINQ, the value seems small. |
Preview 1 of EF8 supports Raw SQL Queries, but we must create class to be able to get a single scalar value.
so this works:
and this throws error:
full example:
Error code
Include provider and version information
EF Core version: 8.0.0-preview.1.23111.4
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.5.1
The text was updated successfully, but these errors were encountered: