Debugging EF Navigation properties yields “ExecuteReader requires and open and available Connection. The connection’s current state is closed.” RRS feed

  • Question

  • When you are debugging and EF entities that have navigation properties that haven’t loaded because of lazy loading, you would normally get something like this in the debugger window:

    The function evaluation requires all threads to run

    Then when you click on the little thread icon, it evaluates this by executing a query to the database and you get something like this:

    Navigation Property Expanded

    Occasionally you will get an exception when you try to evaluate the property:

    Exception when navigating property

    The full stack trace:

    ExecuteReader requires and open and available Connection. The connection’s current state is closed. at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)\r\n at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)\r\n at System.Data.Entity.Core.Objects.ObjectQuery1.<>c__DisplayClass7.<GetResults>b__6()\r\n at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)\r\n at System.Data.Entity.Core.Objects.ObjectQuery1.<>c__DisplayClass7.<GetResults>b__5()\r\n at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 operation)\r\n at System.Data.Entity.Core.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption)\r\n at System.Data.Entity.Core.Objects.ObjectQuery1.Execute(MergeOption mergeOption)\r\n at System.Data.Entity.Core.Objects.DataClasses.EntityReference1.Load(MergeOption mergeOption)\r\n at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.Load()\r\n at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.DeferredLoad()\r\n at System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.LoadProperty[TItem](TItem propertyValue, String relationshipName, String targetRoleName, Boolean mustBeNull, Object wrapperObject)\r\n at System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.<>c__DisplayClass7`2.b__2(TProxy proxy, TItem item)\r\n at System.Data.Entity.DynamicProxies.NameOfProperty_819B7CF59ACBFAC0FE367DDDFF9720DFDEDAB294A8AEAD37D4460065346EAF5F.get_LatestVersion()


    -The issue only ever occurs when debugging and a Navigation property is expanded. The issue never occurs under normal code execution or on a production environment

    -The problem is found in all versions of EF 6.0.x up to the latest 6.1.3

    -All developers report this problem as something that has started occurring in recent history although there is no pattern that can be found as the problem happens on a variety of configurations of Windows 8, 10, Visual Studio 2013, 2015 with various different updates

    -Once the error occurs, any access to the EF context is hampered with errors

    -The error may not occur during the first couple of HTTP requests of a debugging session

    -The error is nothing to do with the DI container as the error is seen on Unity, Autofac and without any DI implemented for the EF context

    -Implementing an interceptor seems to alter the underlying code path so the error doesn’t occur


    Implementing an interceptor will prevent the error from happening. The easiest way of doing this is to use the EF Logging interceptor (Database.Log). This can be set up in the Entity Context’s constructor and can be done by passing in an empty func. We wrap it in an “#if..debug” to make sure it only executes in the debug local environment to save performance:

    public EntityContext()
       #if DEBUG
            Database.Log = s => { };
    Thursday, May 19, 2016 9:17 AM

All replies

  • you have to set your connectionTimeout e.g. 10 minutes in the connection string or in the database and the CommandTimeout for the query. 
    Thursday, May 19, 2016 10:54 AM
  • Yes I've tried it in the Connection String and as well as in  Database.CommandTimeout = 600000.  No change.
    Thursday, May 19, 2016 11:41 AM
  • Hi Kelly Wahl Consulting,

    Could you please provide a simple demo about this issue via OneDrive, I'll try to reproduce your issue on my side and try to find a solution to solve it.

    Best regards,

    Cole Wu

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, May 19, 2016 12:42 PM
  • Thanks Kelly, your workaround worked for me! 


    Friday, July 28, 2017 9:24 PM
  • Can you share any workaround or help please
    Monday, July 2, 2018 8:04 PM