Entity Framework 林克集 QUERY_GOVERNOR_COST_LIMIT

Entity Framework Linq SET QUERY_GOVERNOR_COST_LIMIT

我在 entity framework 中遇到 Linq 查询问题。 我有一个查询,当我 运行 它给出了这个异常

"SqlException: The query has been canceled because the estimated cost of this query (3010) exceeds the configured threshold of 3000. Contact the system administrator"

QUERY_GOVERNOR_COST_LIMIT 的服务器默认值为 3000,如错误中所示。我想更改它并针对特定查询提高它,因为我无法更改服务器的默认值。

所以我的问题是如何在 entity framework Linq 语法中设置 QUERY_GOVERNOR_COST_LIMIT 值。

我已经尝试并添加了这个db.Database.SqlQuery<string>("SET QUERY_GOVERNOR_COST_LIMIT 15000"); 在我的查询之前,但它不起作用,因为如果我没记错的话 QUERY_GOVERNOR_COST_LIMIT 将适用于同一个查询,而不是 2 个不同的查询

SET QUERY_GOVERNOR_COST_LIMIT 正在更改当前数据库连接的限制,因此您需要确保两个 SQL 使用相同的连接。

using (var context = new MyDbContext())
{
    // open connection
    context.Database.Connection.Open();

    // set limit
    context.Database.ExecuteSqlCommand("SET QUERY_GOVERNOR_COST_LIMIT 15000");

    // run query with same connection
    var result = context.Database.SqlQuery<...>("SELECT * ");

}