title: Entity Framework code first connection status checking
body: <p>I am trying to produce a little code, that checks, wheather my SQL server is connectable. And: if so, then it should check if the database is existing.
  Let me explain it via code.</p>
  
  <p>This is my main-method: </p>
  
  <pre><code>private static void Main(string[] args)
          {
              Database.SetInitializer&lt;MyDbContext&gt;(new DropCreateDatabaseAlways&lt;MyDbContext&gt;());
              Console.WriteLine(CheckIfDatabaseExists("Data Source=127.0.0.1\\SQLEXPRESS2;Initial Catalog=SQLTest;Integrated Security=True;Connect Timeout=3"));
              Console.WriteLine(CheckIfDatabaseExists("Data Source=127.0.0.1\\SQLEXPRESS;Initial Catalog=SQLTest;Integrated Security=True;Connect Timeout=3"));
              Console.WriteLine(CheckIfDatabaseExists("Data Source=127.0.0.1\\SQLEXPRESS2;Initial Catalog=SQLTest;Integrated Security=True;Connect Timeout=3"));
          }
  </code></pre>
  
  <p>As you can imagine: there is an existing database 'SQLTest' on my local SQL server 'SQLEXPRESS'.
  But: there is no server like 'SQLEXPRESS2' !</p>
  
  <p>Okay - more code. Here comes my checking method:</p>
  
  <pre><code>public static DatabaseExistsStatus CheckIfDatabaseExists(String connString)
          {
            try
              {
  
                  using (var db = new MyDbContext(connString))
                  {
                      bool DbExists = db.Database.Exists();
                      if (DbExists)
                      {
                          // database is existing
                          return DatabaseExistsStatus.EXISTING;
                      }
                      else
                      {
                          // config is working, but database does not exist
                          return DatabaseExistsStatus.NO_DB;
                      }
                  }
              }
              catch (Exception)
              {
                  // no working config
                  return DatabaseExistsStatus.NO_CONNECTION;
              }
          }
  
          public enum DatabaseExistsStatus
          {
              EXISTING,
              NO_CONNECTION,
              NO_DB
          }
  </code></pre>
  
  <p>Starting my app brings the following result:</p>
  
  <pre><code>NO_CONNECTION
  EXISTING 
  NO_DB
  </code></pre>
  
  <p>I am confused! I expected "NO_CONNECTION, EXISTING, NO_CONNECTION" .</p>
  
  <p>And that's it!I have no idea what is happening in the background and how to get control.</p>
  
  <p>What is happening there and how can I fix that?</p>