2

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))...
pfunk
  • 3,083
  • 4
  • 21
  • 19
  • According to [this post](https://stackoverflow.com/questions/6232633/entity-framework-timeouts), you are using the EF 5 version of how to set the timeout. – Crowcoder Sep 28 '17 at 16:44
  • Well, I checked my NuGet Packages and EF 6.1.3 is what is in the project. I created a totally new project and added an identical model and it generated the same context.cs. I then went into my new project and deleted the old code generation files and did new EF 6 DbContext Generators and it generated identical files. So I don't have a good answer for which version it's really using – pfunk Sep 28 '17 at 17:39
  • This is an old post, but just a question, does the 120 timeout work? if it does, why not changing your code on the `return ""` to `return string.Format("((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 30;");`? – Jaques Aug 10 '18 at 11:40

0 Answers0