title: Database timeout when perfoming first update operation in ADO.NET Entity Framework
body: <p>We are creating an application using WCF services that access database using Entity Framework. We are using SQL Server 2005, .Net 4.0, Entity Framework 4.0 and C#. When the WCF services are deployed, and the application starts, first database operations are always <code>SELECT</code> to read some data and then, upon user interaction with the application some database updates could occur. The problem observed is with the first database <code>UPDATE</code> operation performed after the services are deployed, that normally fails with the error:</p>
  
  <pre><code>An error occurred while updating the entries.
  Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
  </code></pre>
  
  <p>This error raises in the <code>SaveChanges()</code> method and normally this method is enclosed in a transaction like:</p>
  
  <pre><code>// TransactionScopeOption.RequiresNew, IsolationLevel.ReadCommitted, Timeout = TimeSpan.FromMinutes(1)
  using (TransactionScope scope = TransactionScopeUtil.CreateTransactionScope())
  {
    using (var dbcontext = new DBCtx("connectionstring"))
    {
      dbcontext.Connection.Open();
  
      // Read and update database.
  
      dbcontext.SaveChanges();
  
      dbcontext.Connection.Close();
    }
  }
  scope.Complete();
  </code></pre>
  
  <p>This very first database update operation normally fails and then never fail again until services are re-deployed.</p>
  
  <p>Could be a problem with the database connection pool in the cold start?</p>
  
  <p>We are not comfortable with the solution/workaround of increasing the connection/commmand timeout as could mask performance problems in the future and database updates are normally not "big".</p>