Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

entity framework set arithabort on for each query

I have an entity framework query that, when converted to SQL returns within a second, but when ran through entity framework times out after an hour (!) I tracked it down to the fact that, before executing the actual query, entity framework executes:

set arithabort off

I'm looking for either a way to configure EF not to do this, or for a way to override it.

I have tried the following:

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

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

But this executes just once in the beginning, and gets overridden whenever another query is executed.

like image 449
Hugo Forte Avatar asked Jul 15 '15 13:07

Hugo Forte


Video Answer


1 Answers

Thanks to @fiddler, adding an interceptor worked. Feels a bit hackish, but it certainly worked.

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)
    {
    }
}
like image 118
Hugo Forte Avatar answered Nov 04 '22 03:11

Hugo Forte