title: Entity framework: InvalidOperationException: Invalid operation. The connection is closed
body: <p>I using Entity Framework to execute Stored Procedures (Azure SQL Server) in my data-base.</p>
  
  <p>My C# code looks like this:</p>
  
  <pre><code>using (var context = new MyDataContext())
      numberOfEffectedRows = context.MySPName(this.Id);
  </code></pre>
  
  <p>In most cases (99.9%) of the executions, this works fine. But, sometimes it's failed with this error:</p>
  
  <pre><code>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.&lt;&gt;c__DisplayClass4b.&lt;ExecuteFunction&gt;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 ***
  </code></pre>
  
  <p>When this happening:</p>
  
  <ol>
  <li>I don't know why it's happens. It's seems to me like randomized.</li>
  <li>I don't know whether my "MySPName" was executed or not. </li>
  </ol>
  
  <p>Someone known why this happens and how I can know if my Stored Procedure is executed or not?</p>