C# 实体框架&x2B;Postgres:System.InvalidOperationException:连接未打开

C# 实体框架&x2B;Postgres:System.InvalidOperationException:连接未打开,c#,entity-framework,postgresql,nancy,npgsql,C#,Entity Framework,Postgresql,Nancy,Npgsql,我的应用程序最初运行良好,但经过几次请求后,我发现以下错误 我有点不知所措,因为堆栈跟踪没有提供任何线索,说明在我自己的代码中,这可以在哪里修复。有什么想法吗 我在想,EF可能正在某个地方实例化或不正确地关闭连接?虽然我不知道该怎么解决这个问题 System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inne

我的应用程序最初运行良好,但经过几次请求后,我发现以下错误

我有点不知所措,因为堆栈跟踪没有提供任何线索,说明在我自己的代码中,这可以在哪里修复。有什么想法吗

我在想,EF可能正在某个地方实例化或不正确地关闭连接?虽然我不知道该怎么解决这个问题

System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.InvalidOperationException: Connection is not open
  at Npgsql.NpgsqlConnection.CheckReadyAndGetConnector () <0x411d0940 + 0x00097> in :0 
  at (wrapper remoting-invoke-with-check) Npgsql.NpgsqlConnection:CheckReadyAndGetConnector ()
  at Npgsql.NpgsqlCommand.CheckReadyAndGetConnector () <0x411d07b0 + 0x00043> in :0 
  at Npgsql.NpgsqlCommand.ExecuteDbDataReaderInternal (CommandBehavior behavior) <0x411d0690 + 0x0001b> in :0 
  at Npgsql.NpgsqlCommand.ExecuteDbDataReader (CommandBehavior behavior) <0x411d0660 + 0x00013> in :0 
  at System.Data.Common.DbCommand.ExecuteReader (CommandBehavior behavior) <0x414a0ba0 + 0x00018> 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) <0x414a1400 + 0x00027> 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) <0x4104d890 + 0x0010e> in :0 
  at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader (System.Data.Common.DbCommand command, System.Data.Entity.Infrastructure.Interception.DbCommandInterceptionContext interceptionContext) <0x414a1060 + 0x00263> in :0 
  at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader (CommandBehavior behavior) <0x414a0bd0 + 0x000e6> in :0 
  at System.Data.Common.DbCommand.ExecuteReader (CommandBehavior behavior) <0x414a0ba0 + 0x00018> 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) <0x4149bbf0 + 0x00043> 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) <0x4149bbf0 + 0x000b3> 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) <0x41498580 + 0x000a4> in :0 
  --- End of inner exception stack trace ---
  at Nancy.NancyEngine.InvokeOnErrorHook (Nancy.NancyContext context, Nancy.ErrorPipeline pipeline, System.Exception ex) <0x41567750 + 0x002c3> in :0 
这就是我实例化的一个连接:

public static DbConnection EntityFrameworkConnection
{
    get
    {
        var conn = Npgsql.NpgsqlFactory.Instance.CreateConnection();
        conn.ConnectionString = ConnectionString;
        return conn;
    }
}

“ConnectionString”是包含实际连接字符串的静态字符串。

上下文初始值设定项是否存在差异?你怎么称呼构造器?ContextOwnsConnection是真的吗?DbContext的构造函数还是实体的构造函数?我已经将DbContext的构造函数添加到了我的原始问题中。是,ContextOwnsConnection设置为true。请在返回之前尝试打开连接。可能是在上下文初始化期间首先打开连接,但在检索和查询执行之间会出现超时。因此,您的意思是在
公共静态数据库连接EntityFrameworkConnection
中打开连接?这是否也考虑到我的应用程序最初运行良好,在几个请求后出现错误?只是想弄清楚问题的根源。。。谢谢
public static DbConnection EntityFrameworkConnection
{
    get
    {
        var conn = Npgsql.NpgsqlFactory.Instance.CreateConnection();
        conn.ConnectionString = ConnectionString;
        return conn;
    }
}