title: OPTIMIZE FOR UNKNOWN on specific EF Core query
body: <p>I have a webjob project running on .NET Framework with EF Core 3.1.
  The webjob processes messages from an Azure Service Bus and saves them into an Azure SQL Database.</p>
  <p>The problem I have is that the Azure SQL Database generates really bad query plans for the query that EF Core generated. With the generated query plan the execution time is 1-2 minutes. However when I use <code>OPTION (OPTIMIZE FOR UNKNOWN)</code> the execution time drops down to 0.01 - 0.02 minutes.</p>
  <p>So now I want to implement the <code>OPTION (OPTIMIZE FOR UNKNOWN)</code> in EF Core. I have found that they added a <code>DbCommandInterceptor</code> in EF Core 3.1 where can you append things to your query: <a href="https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.x/#interception-of-database-operations" rel="nofollow noreferrer">MSDOCS</a></p>
  <pre class="lang-c# prettyprint-override"><code>public class HintCommandInterceptor : DbCommandInterceptor
  {
      public override InterceptionResult&lt;DbDataReader&gt; ReaderExecuting(
          DbCommand command,
          CommandEventData eventData,
          InterceptionResult&lt;DbDataReader&gt; result)
      {
          // Manipulate the command text, etc. here...
          command.CommandText += &quot; OPTION (OPTIMIZE FOR UNKNOWN)&quot;;
          return result;
      }
  }
  </code></pre>
  <p>But it seems like this interceptor will run on every query and I only want it for a specific query.
  I could implement a seperate DbContext for this interceptor but that doesn't seem like a solid solution.
  Does anyone have an idea how I could implement this in a correct way?</p>