C# 实体框架:InvalidOperationException:无效操作。连接已关闭

C# 实体框架:InvalidOperationException:无效操作。连接已关闭,c#,entity-framework,C#,Entity Framework,我使用实体框架在数据库中执行存储过程(Azure SQL Server) 我的C#代码如下所示: using (var context = new MyDataContext()) numberOfEffectedRows = context.MySPName(this.Id); 在大多数情况下(99.9%)的死刑执行中,这种方法效果良好。但是,有时会出现以下错误: System.InvalidOperationException: Invalid operation. The con

我使用实体框架在数据库中执行存储过程(Azure SQL Server)

我的C#代码如下所示:

using (var context = new MyDataContext())
    numberOfEffectedRows = context.MySPName(this.Id);
在大多数情况下(99.9%)的死刑执行中,这种方法效果良好。但是,有时会出现以下错误:

System.InvalidOperationException: Invalid operation. The connection is closed.
   at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
   at System.Data.SqlClient.SqlInternalTransaction.Rollback()
   at System.Data.SqlClient.SqlInternalTransaction.Dispose(Boolean disposing)
   at System.Data.SqlClient.SqlTransaction.Dispose(Boolean disposing)
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   at System.Data.Entity.Infrastructure.Interception.DbTransactionDispatcher.Dispose(DbTransaction transaction, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Core.EntityClient.EntityTransaction.Dispose(Boolean disposing)
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass4b.<ExecuteFunction>b__49()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteFunction(String functionName, ObjectParameter[] parameters)
   at ***.MyDataContext.MySPName(Nullable`1 Id) in ***
   at ***.DoSomething() in ***
System.InvalidOperationException:操作无效。连接已关闭。
位于System.Data.SqlClient.SqlInternalConnectionDS.ValidateConnectionForExecute(SqlCommand)
位于System.Data.SqlClient.SqlInternalTransaction.Rollback()处
位于System.Data.SqlClient.SqlInternalTransaction.Dispose(布尔disposing)
位于System.Data.SqlClient.SqlTransaction.Dispose(布尔处理)
在System.Data.Entity.Infrastructure.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget,Action`2 operation,TInterceptionContext interceptionContext,Action`3 executing,Action`3 executed)
位于System.Data.Entity.Infrastructure.Interception.DbTransactionDispatcher.Dispose(DbTransaction事务,DbInterceptionContext interceptionContext)
位于System.Data.Entity.Core.EntityClient.EntityTransaction.Dispose(布尔处理)
在System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 Func,IDBEcutionStrategy executionStrategy,Boolean startLocalTransaction,Boolean releaseConnectionOnSuccess)
在System.Data.Entity.Core.Objects.ObjectContext.c__DisplayClass4b.b__49()中
在System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1操作)中
位于System.Data.Entity.Core.Objects.ObjectContext.ExecuteFunction(字符串函数名,ObjectParameter[]参数)
在中的***.MyDataContext.MySPName(可为null的'1 Id)***
在***
发生这种情况时:

  • 我不知道为什么会这样。在我看来这是随机的
  • 我不知道我的“我的名字”是否被执行了
    有人知道为什么会发生这种情况,以及我如何知道是否执行了存储过程?

    Linq查询是惰性地进行计算的。查询将在您第一次访问numberOfEffectedRows时运行。由于在创建数据上下文时使用了“using”,所以一旦超出范围,它就会被释放。如果您访问的numberOfEffectedRows超出了使用范围,则会发生这种情况,因为没有有效的数据上下文,查询无法运行。

    连接因超时而关闭,因此建议您检查连接字符串中的连接超时

     connectionString="Data Source=..;Initial Catalog=;Persist Security Info=..;User ID=..;Password=..;Connect Timeout=.."
    

    对于诊断,我建议您运行
    SQL Server Profiler

    EntityFramework上下文不是线程安全的,可能是由于某些奇怪的原因,数据上下文在执行过程中被关闭

    处理这种意外的一种方法是为每个请求提供一个上下文

    这是最简单的解决方案,因此您“希望”永远不会再遇到此异常

    检查这里

    你能做的就是

    container.RegisterType<IDataContext, MyDataContext>(new PerRequestLifetimeManager()); 
    
    container.RegisterType(新的PerRequestLifetimeManager());
    
    然后将DataContext注入需要访问的任何位置


    这样,当容器被释放时,上下文将自动关闭

    粘贴的代码段是否引发异常?是。根据堆栈跟踪代码行“context.MySPName(this.Id);”是异常的源。@NO1LIVES4请确保在离开using语句之前枚举查询,EF使用DEREFERED executionnumberOfEffectedRows是int。按值复制。我的代码中没有LINQ。将值复制到“numberOfEffectedRows”是“按值”完成的。我仍然认为这与您在创建数据上下文时使用的语句有关,因为堆栈跟踪中有所有这些dispose方法调用,然后它尝试验证连接。您如何知道这是由于超时造成的?