title: Cannot create file '*.mdf' because it already exists
body: <p>I am using <strong>Entity-Framework</strong> in a <strong>Winforms</strong> application. The application connects to <strong>MSSQLServer</strong> and everything works fine. Then I detach the database and copy the <code>.mdf</code> file to the app folder and change the connection string to use the local <code>.mdf</code> file, still everything works fine.</p>
  
  <p>The problem is that when I copy my application together with the database to another PC suddenly EF tries to recreate the database and throws this error:</p>
  
  <blockquote>
    <p>Cannot create file <strong>'Path\MyDatabaseName.mdf'</strong> because it already exists.
    Change the file path or the file name, and retry the operation. CREATE
    DATABASE failed. Some file names listed could not be created. Check
    related errors.</p>
  </blockquote>
  
  <p>Context Initializer is set to <code>MigrateDatabaseToLatestVersion</code>:</p>
  
  <pre><code>Database.SetInitializer&lt;MyContext&gt;(new MigrateDatabaseToLatestVersion&lt;MyContext, DAL.Migrations.Configuration&gt;("MyConnectionString"));
  </code></pre>
  
  <p>Also, automatic migration is off and database is created by EF Code-First on Sql-server and is already updated to the latest migration so no migration is needed.</p>
  
  <p>Connection string for the first scenario is:</p>
  
  <pre><code>&lt;add name="MyConnectionString" connectionString="Server=DESKTOP-XXXXXXX; Database=MyDatabase; Integrated Security=True; Connect Timeout=30;" providerName="System.Data.SqlClient"&gt;
  </code></pre>
  
  <p>and for local <code>.mdf</code> file is:</p>
  
  <pre><code>&lt;add name="MyConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB; AttachDbFilename=|DataDirectory|\MyDatabase.mdf; Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient" /&gt;
  </code></pre>
  
  <p>So, What is going on here??</p>