title: EntityFramework + Postgres: System.InvalidOperationException: Connection is not open
body: <p>My app runs fine initially but after a couple of requests, I get the error below.</p>
  
  <p>I am a bit lost as the stack trace gives no clue as to where in my own code this could be fixed. Any ideas?</p>
  
  <p>I was thinking that somewhere EF is instantiating or closing the connection improperly perhaps? Although I have no clue where to fix that.</p>
  
  <pre><code>System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---&gt; System.InvalidOperationException: Connection is not open
    at Npgsql.NpgsqlConnection.CheckReadyAndGetConnector () &lt;0x411d0940 + 0x00097&gt; in :0 
    at (wrapper remoting-invoke-with-check) Npgsql.NpgsqlConnection:CheckReadyAndGetConnector ()
    at Npgsql.NpgsqlCommand.CheckReadyAndGetConnector () &lt;0x411d07b0 + 0x00043&gt; in :0 
    at Npgsql.NpgsqlCommand.ExecuteDbDataReaderInternal (CommandBehavior behavior) &lt;0x411d0690 + 0x0001b&gt; in :0 
    at Npgsql.NpgsqlCommand.ExecuteDbDataReader (CommandBehavior behavior) &lt;0x411d0660 + 0x00013&gt; in :0 
    at System.Data.Common.DbCommand.ExecuteReader (CommandBehavior behavior) &lt;0x414a0ba0 + 0x00018&gt; in :0 
    at (wrapper remoting-invoke-with-check) System.Data.Common.DbCommand:ExecuteReader (System.Data.CommandBehavior)
    at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.b__c (System.Data.Common.DbCommand t, System.Data.Entity.Infrastructure.Interception.DbCommandInterceptionContext`1 c) &lt;0x414a1400 + 0x00027&gt; in :0 
    at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1[TInterceptor].Dispatch[TTarget,TInterceptionContext,TResult] (System.Data.Entity.Infrastructure.Interception.TTarget target, System.Func`3 operation, System.Data.Entity.Infrastructure.Interception.TInterceptionContext interceptionContext, System.Action`3 executing, System.Action`3 executed) &lt;0x4104d890 + 0x0010e&gt; in :0 
    at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader (System.Data.Common.DbCommand command, System.Data.Entity.Infrastructure.Interception.DbCommandInterceptionContext interceptionContext) &lt;0x414a1060 + 0x00263&gt; in :0 
    at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader (CommandBehavior behavior) &lt;0x414a0bd0 + 0x000e6&gt; in :0 
    at System.Data.Common.DbCommand.ExecuteReader (CommandBehavior behavior) &lt;0x414a0ba0 + 0x00018&gt; in :0 
    at (wrapper remoting-invoke-with-check) System.Data.Common.DbCommand:ExecuteReader (System.Data.CommandBehavior)
    at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands (System.Data.Entity.Core.EntityClient.EntityCommand entityCommand, CommandBehavior behavior) &lt;0x4149bbf0 + 0x00043&gt; in :0 
    --- End of inner exception stack trace ---
    at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands (System.Data.Entity.Core.EntityClient.EntityCommand entityCommand, CommandBehavior behavior) &lt;0x4149bbf0 + 0x000b3&gt; in :0 
    at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType] (System.Data.Entity.Core.Objects.ObjectContext context, System.Data.Entity.Core.Objects.ObjectParameterCollection parameterValues) &lt;0x41498580 + 0x000a4&gt; in :0 
    --- End of inner exception stack trace ---
    at Nancy.NancyEngine.InvokeOnErrorHook (Nancy.NancyContext context, Nancy.ErrorPipeline pipeline, System.Exception ex) &lt;0x41567750 + 0x002c3&gt; in :0 
  </code></pre>
  
  <p>Any insight is appreciated. Thanks! :-)</p>
  
  <p>===== EDIT =====</p>
  
  <pre><code>public ReflectDbContext() : base(ReflectConfiguration.EntityFrameworkConnection, true) {
  //          Database.Log = s =&gt; System.Diagnostics.Debug.WriteLine(s);
  }
  </code></pre>
  
  <p>And this is know I instantiate a connection:</p>
  
  <pre><code>public static DbConnection EntityFrameworkConnection
  {
      get
      {
          var conn = Npgsql.NpgsqlFactory.Instance.CreateConnection();
          conn.ConnectionString = ConnectionString;
          return conn;
      }
  }
  </code></pre>
  
  <p>"ConnectionString" is a static containing the actual connection string.</p>