I'm confused !
I have an ASP.Net Core 3 WebApi application, and this works fine:
var results = _context.Users.ToList();
However, if I try to add a "HINT" to the SQL...
var results = _context.Users.FromSqlRaw("SELECT * FROM t_user OPTION(USE HINT('ENABLE_PARALLEL_PLAN_PREFERENCE'))").ToList();
... then it throws this exception...
Incorrect syntax near the keyword 'OPTION'.
Database 'HINT' does not exist. Make sure that the name is entered correctly.
Why would this SQL run successfully in SQL Server Management Studio, but get misunderstood by the WebApi ?
Update # 1
Damn. When you tell EF Core to use Raw SQL, it actually puts that SQL into a sub-clause, and this is why I'm seeing the error.
So, this is the (perfectly valid) SQL which I'm trying to run:
SELECT * FROM t_user
OPTION(USE HINT('ENABLE_PARALLEL_PLAN_PREFERENCE'))
..but, looking at SQL Server Profiler, EF Core is actually trying to this SQL, and this isn't valid...
SELECT [u].[user_id], [u].[user_name]
FROM (
SELECT * FROM t_user
OPTION(USE HINT('ENABLE_PARALLEL_PLAN_PREFERENCE'))
) AS [u]
Damn...
So how do we use Hints from EF Core ?
Update # 2
Because I'm using EF Core, I followed the instructions in this Microsoft article to add a DbCommandInterceptor to my query.
In their example, it intercepts the SQL and appends " OPTION (ROBUST PLAN)" to the string. It shows that it's trying to run this SQL:
SELECT [u].[user_id], [u].[user_name]
FROM t_user
OPTION (ROBUST PLAN)
If I take the Microsoft example, and change it to use my HINT, then I still get the same error of it saying "Database 'HINT' does not exist."
private static void ManipulateCommand(DbCommand command)
{
if (command.CommandText.StartsWith("-- Use hint: robust plan", StringComparison.Ordinal))
{
command.CommandText += " OPTION(USE HINT('ENABLE_PARALLEL_PLAN_PREFERENCE'))";
}
}
Ahhhh! Why can't I use a Hint ?!