实体框架为每个查询设置arithabort

Hug*_*rte 4 entity-framework-6

我有一个实体框架查询,当转换为SQL在一秒钟内返回时,但是当在一小时后运行实体框架时超时(!)我追踪到这样的事实:在执行实际查询之前,实体框架执行:

set arithabort off
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种方法来配置EF不要这样做,或者寻找一种方法来覆盖它.

我尝试过以下方法:

public partial class MyContext : DbContext
{
    public MyContext () : base("name=MyContext ")
    {
       Context.Database.ExecuteSqlCommand("set arithabort on");
    }

    public DbContext Context
    {
        get { return this; }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是这在开始时只执行一次,并且每当执行另一个查询时都会被覆盖.

Hug*_*rte 6

感谢@fiddler,添加了拦截器.感觉有点hackish,但它肯定有效.

public partial class IfcContext : DbContext, IIfcContext
{
    public MyContext() : base("name=MyContext")
    {
        ///used to set ArithAbort to on before each query
        DbInterception.Add(new Interceptor());
    }

    public DbContext Context
    {
        get { return this; }
    }
}


public class Interceptor : IDbCommandInterceptor
{

    public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
    {
        command.CommandText = "SET ARITHABORT ON; " + command.CommandText;
    }

    public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
    }

    public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
    }

    public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
    {

    }

    public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
    }

    public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)