EntityFramework + Postgres:System.InvalidOperationException:连接未打开

时间:2016-08-22 11:55:26

标签: c# entity-framework postgresql nancy npgsql

我的应用程序最初运行正常,但在几次请求后,我收到以下错误。

我有点迷失,因为堆栈跟踪没有提供关于我自己的代码中哪些可以修复的线索。有什么想法吗?

我在想某个地方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 ReflectDbContext() : base(ReflectConfiguration.EntityFrameworkConnection, true) {
//          Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
}

这就知道我实例化了一个连接:

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

&#34;的ConnectionString&#34;是包含实际连接字符串的静态。

0 个答案:

没有答案