Background:
Ran across a problem in our production system where Entity Framework queries are NOT timing out after 30 seconds. We are using SQL Server 2014.
At one point we were seeing timeouts (ie if I ran a sql profiler trace we would see it run for 30 seconds and then throw a timeout error). But recently those aren't getting generated and I am seeing a couple problematic queries running very long. I don't have a specific time or change when we stopped seeing them.
This is a database first context. Our Context.tt file has been modified to alter the CommandTimeout for two stored procedures to 120 seconds and for 6 others to 0 (infinite timeout).
public string FunctionTimeout(EdmFunction edmFunction, string modelNamespace)
{
if (edmFunction.Name == "StoredProc1" || edmFunction.Name == "StoredProc2")
return string.Format("((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 120;");
else if (edmFunction.Name == "StoredProc3" || edmFunction.Name == "StoredProc4" )
return string.Format("((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 0;");
else
return "";
}
Our dbcontext is registered in Autofac as follows
var builder = new ContainerBuilder();
builder.RegisterType<MyContext>().AsSelf().InstancePerLifetimeScope();
This is being run in a Windows Service, but the data layer is also shared with a web api project.
.NET Framework 4.5.2
Entity Framework 6.1.3
Autofac 4.6.1
So question time:
1) Is setting the CommandTimeout causing it to be set at a "global" level? (ie once our system happens to call one of the stored procs above then every EF call after that has no timeout or a 2 minute timeout)
2) Is there a hook (in the template or elsewhere) to set it back to 30 secs before a regular EF call (ie before a LINQ query on a regular entity), not just stored procs (ie adding a return value on the else statement above)?
3) Would adding the CommandTimeout to the constructor help?
public <#=code.Escape(container)#>()
: base("name=<#=container.Name#>")
{
((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 30;
<#
if (!loader.IsLazyLoadingEnabled(container))...