title: ASP.NET Core: An attempt to attach an auto-named database for file failed
body: <p>I know this question has been asked many times, but I'm still unable to find a question similar to my scenario.</p>
  
  <p>I'm working on an ASP.NET Core WebAPI project.
  It all worked well and I can't really reproduce what if at all I did to screw this up.</p>
  
  <p>It's supposed to debug using this connection string, which is retrieved from the <em>appsettings.Development.json</em> file:</p>
  
  <pre><code>"DefaultConnection":
    "Data Source=(localdb)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|MyProj.mdf"
  </code></pre>
  
  <p>When calling <code>context.Database.CreateIfNotExists();</code>, I get the following error:</p>
  
  <p>System.Data.SqlClient.SqlException occurred (HResult 0x80131904):</p>
  
  <blockquote>
    <p>An attempt to attach an auto-named database for file C:\Users\Shimmy\Documents\Visual Studio 2017\Projects\MyProj\MyProj.Api\AppData\MyProj.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.<br>
    <strong>Source</strong>: .Net SqlClient Data Provider<br>
    <strong>StackTrace</strong>:
       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)</p>
  </blockquote>
  
  <p>I'm running the WebAPI as a Kestrel console app.</p>
  
  <p>Here's what I've tried:  </p>
  
  <ul>
  <li>Adding <code>Integrated Security=True</code> or <code>MultipleActiveResultSets=True</code> to the connection string</li>
  <li>Added the full path to the connection string</li>
  <li>Deleting and recreating the <em>MSSQLLocalDB</em> instance</li>
  <li>Reinstalling SQL LocalDB</li>
  </ul>
  
  <p>Replacing the <code>AttachDbFilename</code> flag with <code>Initial Catalog=MyProj</code>, raised this error:</p>
  
  <blockquote>
    <p>System.Data.SqlClient.SqlException occurred<br>
    <strong>HResult</strong>: 0x80131904<br>
    <strong>Message</strong>: Cannot open database "MyProj" requested by the login. The login failed.
    Login failed for user 'SHIMMY-PC\Shimmy'.<br>
    <strong>Source</strong>: .Net SqlClient Data Provider<br>
    <strong>StackTrace</strong>:
       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)</p>
  </blockquote>
  
  <p>But <em>SHIMMY-PC\Shimmy</em> does appear to be in the instance's users:</p>
  
  <p><a href="https://i.stack.imgur.com/4616r.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4616r.png" alt="enter image description here"></a></p>