Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework: InvalidOperationException: Invalid operation. The connection is closed

I using Entity Framework to execute Stored Procedures (Azure SQL Server) in my data-base.

My C# code looks like this:

using (var context = new MyDataContext())
    numberOfEffectedRows = context.MySPName(this.Id);

In most cases (99.9%) of the executions, this works fine. But, sometimes it's failed with this error:

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 ***

When this happening:

  1. I don't know why it's happens. It's seems to me like randomized.
  2. I don't know whether my "MySPName" was executed or not.

Someone known why this happens and how I can know if my Stored Procedure is executed or not?

like image 691
No1Lives4Ever Avatar asked Jan 06 '16 15:01

No1Lives4Ever


1 Answers

The connection is closed is due to timeout, so suggest you to check your Connect Timeout in your connection string

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

For diagnostic i suggest you to run SQL Server Profiler

like image 147
Aghilas Yakoub Avatar answered Nov 01 '22 13:11

Aghilas Yakoub