I'm currently doing a complex migration task where each iteration must be wrapped in a transaction in case of failure. Each iteration can take as long as 2 hours.
This has mostly been successful. There have been some cases where a long operation is required in between accesses to the connection.
Essentially the connection is idle for a while (> 10 minutes) and when I try to use the connection again I discover it is broken:
System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: The connection is broken and recovery is not possible. The connection is marked by the server as unrecoverable. No attempt was made to restore the connection.
at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__180_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
The code looks something like:
// Context = instance of DbContext
using (var dbContextTransaction = Context.Database.BeginTransaction())
{
DoStuffWithContext(Context);
PotentiallyLongOperationWithoutContext();
DoMoreStuffWithContext(Context);
}
Normally this all works fine but when PotentiallyLongOperationWithoutContext does take a long time I get the exception when DoMoreStuffWithContext first tries to use the context.