How to add OPTION RECOMPILE into Entity Framework -


i experiencing parameter sniffing problem sometimes. add option (recompile) end of query fix it. how can in ef 6?

i implemented idbcommandinterceptor interface fix parameter sniffing error.

the usage of new interceptor:

  using (var datacontext = new adventureworks2012entities())   {     var optionrecompileinterceptor = new optionrecompileinterceptor();     dbinterception.add(optionrecompileinterceptor);      string city = "seattle";     var seattle = (       in datacontext.addresses       a.city == city       select a).tolist();      dbinterception.remove(optionrecompileinterceptor); //remove interceptor not affect other queries   } 

implementation of interceptor:

public class optionrecompileinterceptor : dbcommandinterceptor {   static void addoptiontocommand(dbcommand command)   {     string optionrecompilestring = "\r\noption (recompile)";     if (!command.commandtext.contains(optionrecompilestring)) //check option not added     {       command.commandtext += optionrecompilestring;     }   }     public override void nonqueryexecuting(     dbcommand command, dbcommandinterceptioncontext<int> interceptioncontext)   {     addoptiontocommand(command);   }    public override void readerexecuting(     dbcommand command, dbcommandinterceptioncontext<dbdatareader> interceptioncontext)   {     addoptiontocommand(command);   }    public override void scalarexecuting(     dbcommand command, dbcommandinterceptioncontext<object> interceptioncontext)   {     addoptiontocommand(command);   } } 

Comments

Popular posts from this blog

sql - Time in datatime field -

javascript - How to split the success output from an Ajax post? -

java - sharing object across different classes -