• I'd suggest reading this article by Erland Sommerskag, http://www.sommarskog.se/query-plan-mysteries.html. It explains how different SET OPTIONS are used by different providers and can affect query compilation. I'd be willing to bet this is partly why the optimizer chooses a better plan in SSMS.

    Also running the query in SSMS using a constant or a variable is not the same as a query being run using parameters as parameter-sniffing is not occurring. You can try using sp_executesql in SSMS to run the query and pass the value as a parameter and not a variable and you may see different results, I know I have in the past.