title: Cannot create database file programmatically (SQL Server 2008 R2 Express)
body: <p>I'm trying to create a database with LINQ to SQL programmatically.</p>
  
  <p>I tried (following <a href="http://msdn.microsoft.com/en-us/library/bb399420%28v=VS.90%29.aspx" rel="nofollow noreferrer">this</a>):</p>
  
  <pre><code>MySQLSvrDb db = new MySQLSvrDb(@"c:\mydb.mdf");
  if (!db.DatabaseExists())
  {
      db.CreateDatabase();
  }
  </code></pre>
  
  <p>But I get a SQLException "A database with the same name exists, or specified file cannot be opened, or it is located on UNC share." </p>
  
  <p>However, for unit tests I created a database the following way (making sure I have a empty db for every test):</p>
  
  <pre><code>MySQLSvrDb db = new MySQLSvrDb(@"C:\testdb.mdf");
  if (db.DatabaseExists())
  {
      Console.WriteLine("Deleting old database...");
      db.DeleteDatabase();
  }
  db.CreateDatabase();
  </code></pre>
  
  <p>This works fine. My problem is I don't see a difference to the first approach. The problem is maybe somehow related to <a href="https://stackoverflow.com/questions/2796013/datacontext-createdatabase-says-file-already-exists-but-it-doesnt">this</a>, but the suggested solutions didn't work.</p>
  
  <p>Any hints?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>If I just skip the DatabaseExists() step it works, but I need to check, if there's already a Db.</p>