An attempt to attach an auto-named database for file ....database1.mdf failed An attempt to attach an auto-named database for file ....database1.mdf failed asp.net asp.net

An attempt to attach an auto-named database for file ....database1.mdf failed


I had this problem also and it was a pain. I configured my connection string and managed to solve the problem. In the connection string I replaced the value |DataDirectory|\dbfilename.mdf for AttachDbFilename property, with the path to the file. |DataDirectory| can only be used if the database file is in the App_Data folder inside same project.

So changing the AttachDbFilename property to the direct path of the mdf file, fixed my issue.

AttachDbFilename=C:\MyApp\App\DAL\db.mdf

I hope this works for you.


Try to create your connection string as below:

 <add name="ECommerce" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=C:\USERS\dL\DESKTOP\DATABASE\MYSHOP.MDF;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False" providerName="System.Data.SqlClient"/>

It's working for me.


<add name="Connections" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>