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

Show Details

Solution 1: [1]

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.


I hope this works for you.

Solution 2: [2]

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.

Solution 3: [3]

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

Solution 4: [4]

I had a similar error, but using a connection configured in code, rather than a config file. The solution for me was simply to add the "Initial Catalog=MyDatabase" part. My code now looks like...

DbConnection connection = new SqlConnection(String.Format(@"Data Source=.\SQLEXPRESS; Integrated Security=SSPI; AttachDbFilename={0}; User Instance=True; Initial Catalog=IPDatabase;", Location));

Location is a full path to an mdb file, that does not have to exist yet.

Solution 5: [5]

One of the most annoying errors when developing web apps in .Net. I had this issue on a project I was doing a year ago. This is what worked for me: I've logged in with sa account to SQL Management Studio and attached the database to the object explorer (Databases -> Right Click -> Attach). Then I've opened Security->Logins->[myWindowsUsername] and edited User Mappings tab, giving dbowner rights to the attached database.

I'm sure you can do those things without Management Studio, with console commands, but I'm not that good with T-SQL and can't give you advice on that.


This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Credit
Solution 1 ray abu
Solution 2 user3198801
Solution 3 Radha
Solution 4 flobadob
Solution 5 george.zakaryan