Search code examples
c#entity-frameworkentity-framework-6timeoutdatabase-connection

Command Timeout being ignored


I use this function to check that the connection to the database server works, I've set a timeout of 3 seconds, but when the server is not available, it takes about 45 seconds to catch the error. Why is CommandTimeout being ignored?

bool CheckDbConn()
{
  try
  {
    using (MyDB db=new MyDB(GetCustomConnString()))
    {
      db.Database.CommandTimeout = 3;
      var someEntity =  db.SomeSet.FirstOrDefault();
      return true; // connection ok
    }
  }
  catch (Exception ex)
  {
   // catches error after 45 seconds
   return false; // error connecting server;
  }
}
  • Entity Framework 6.2

Solution

  • What you need is ConnectionTimeout

    CommandTimeout if for limiting how long your command runs.