微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 实体框架:InvalidOperationException:无效的操作.连接已关闭

我使用Entity Framework在我的数据库中执行存储过程(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 ***

发生这种情况时:

>我不知道为什么会这样.在我看来,这似乎是随机的.
>我不知道我的“MySPName”是否被执行了.

有人知道为什么会这样,以及我怎么知道我的存储过程是否被执行?

解决方法

连接已关闭是由于超时,因此建议您检查连接字符串中的连接超时

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐