Stored Procedures in EF Core 3.0
Microsoft.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near the keyword 'EXECUTE'. Incorrect syntax near ')'.
For the above error, we should use .ToList() or .ToListAsync() not .FirstOrDefault() or .FirstOrDefaultAsync()
It will work
var user = await _context.Set<User>().FromSql("EXECUTE dbo.spTest").ToListAsync();
It won't work
var user = await _context.Set<User>().FromSql("EXECUTE dbo.spTest").FirstOrDefaultAsync();
/*
Transalated SQL:
SELECT TOP(1) [u].[FullName], [u].[Password], [u].[UserName]
FROM (
EXECUTE dbo.spTest
) AS [u]
*/
The accepted answer nails it. Here are my two cents however:
Also, if you want to get only one result and still want to make the server call asynchronously:
var user = (await _context.Set<User>().FromSql("EXECUTE dbo.spTest").ToListAsync()).FirstOrDefault();