Cannot attach the file *.mdf as database

error number:1832,state:1,class:14
attach mdf file in visual studio
cannot attach database
mdf file not attach database
cannot attach a database with the same name
an attempt to attach an auto-named database for file
cannot attach a database that was being restored

Basically I've followed a tutorial and decided to delete the .mdf file afterwards.

Now whenever I try to run the application I get the following error (the title of this thread). The code where I get the error is shown below (ASP.NET MVC 4):

OdeToFoodDB db = new OdeToFoodDB();

public ActionResult Index()
{
    var model = db.Restaurants.ToList();
    return View(model);
}

My connection string is the following:

<add name="DefaultConnection" 
     connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=OdeToFoodDb;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\OdeToFoodDb.mdf" 
     providerName="System.Data.SqlClient" />

I've tried looking at the SQL Server Object Explorer but it looks the following:

Also, in Server Explorer I don't see any data connections.

And when I try to add a new connection in Server Explorer I don't see any databases named OdeToFoodDb.

Sorry for this wide question but I'm new to Entity Framework and don't quite get what's wrong here.

Take a look at this: Entity Framework don't create database

I would try giving the database a different name. Sometimes you can run into problems with SQL Express when trying to create a database with the same name a second time. There is a way to fix this using SQL Server Management Studio but it's generally easier to just use a different database name.

Edit This answer was accepted because it confirms the bug and the workaround used by OP (renaming database could help). I totally agree that renaming the database is not really an acceptable way, and does not totally solve the issue. Unfortunatly I didn't check the other ways to really solve it in SSMS.

[SOLVED], I am trying to add a new table to my existing database , when I try updating the database on the package manager console I get an error which  Reason: happens because you deleted the backing files .mdf, ldf without actually deleting the database within the running instance of SqlLocalDb; re-running the code in VS won't help because you cannot re-create a DB with the same name (and that's why renaming works, but leaves the old phantom db name lying around).

I think that for SQL Server Local Db you shouldn't use the Initial Catalog property. I suggest to use:

<add name="DefaultConnection" 
     connectionString="Data Source=(LocalDb)\v11.0;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\OdeToFoodDb.mdf" 
     providerName="System.Data.SqlClient" />

I think that local db doesn't support multiple database on the same mdf file so specify an initial catalog is not supported (or not well supported and I have some strange errors).

Cannot attach the file 'C:…database.mdf' as database x – Entity , Cannot attach the file 'D\\mydatabasename.mdf' as database 'mydatabasename​.mdf' [Answered]RSS. 5 replies. Last post Jul 20, 2018 11:42  Cannot attach the file “.mdf” as database “aspnet-” Attach (open) mdf file database with SQL Server Management Studio. 0. Cannot attach mdf file as

  1. From Package Manager Console run:

    sqllocaldb.exe stop v11.0

    sqllocaldb.exe delete v11.0

  2. Run your project

  3. Register a user

Cannot attach the file *.mdf as database, SqlException (0x80131904): Cannot attach the file '.\OurDbName.mdf' as database 'OurDbName'. Just wanted to express that here for other  System.Data.SqlClient.SqlException (0x80131904): Cannot attach the file 'E:\Backup\Practice\MVC4\DotNetExamples\DotNetExamples\App_Data\DotnetExamples.mdf' as database 'DotnetExamples'. Solution: If you delete the DB file, it still stays registered with SqlLocalDB.

Remove this line from the connection string that should do it ;) "AttachDbFilename=|DataDirectory|whateverurdatabasenameis-xxxxxxxxxx.mdf"

Cannot attach the file 'D\\mydatabasename.mdf' as database , SqlException (0x80131904): Cannot attach the file '.\OurDbName.mdf' as database 'OurDbName'. Just wanted to express that here for other  MDF File Location Displays the path and file name of the selected MDF file. Database Name Displays the name of the database. Attach As Optionally, specifies a different name for the database to attach as. Owner Provides a drop-down list of possible database owners from which you can optionally select a different owner.

"Cannot attach the file 'C:\Github\TestService\TestService\App_data\TestService.mdf" as database 'TestService'

When you meet the above error message, Please do the following steps.

  1. Open SQL Server Object Explorer
  2. Click refresh button.
  3. Expand (localdb)\MSSQLLocalDB(SQL Server 12.x.xxxxx - xxxxx\xxxx)
  4. Expand Database
  5. Please remove existed same name database
  6. Click right button and then delete
  7. Go back to your Package Manage Console
  8. Update-Database

Update database against LocalDB does not work · Issue #6847 , LocalDb error: Cannot attach the file 'sample.mdf' as database 'sample.mdf'. In a demo app I used the LocalDB functionality to host my  Reason: happens because you deleted the backing files .mdf, ldf without actually deleting the database within the running instance of SqlLocalDb; re-running the code in VS won’t help because you cannot re-create a DB with the same name (and that’s why renaming works, but leaves the old phantom db name lying around).

Error when specifying AttachDbFileName in connection string · Issue , Cannot attach the file '…\App_Data\DepartmentDb.mdf' as database '​DepartmentDb'. Or the following error in a desktop app: SqlException:  For SQLAdminTwo to be able to reattach the database, full permissions on the mdf and ldf needs to be granted to either the Database Engine SID NT SERVICE\MSSQLSERVER or the Windows login of the DBA who attempts to reattach the database. In this tip, SQLAdminTwo is granted full privileges on the database files.

LocalDb error: Cannot attach the file 'sample , Cannot attach the file *.mdf as database problem: You get an error such as '​Cannot attach the file 'YourDB.mdf' as database 'YourConnStringNamedContext';​  Solution :- Give FULL Control to the SQL Service account on the folder where .mdf , .ndf and log files staying. Note:- If SQL Service account can not be added to network path folder in that case give Full Control to Everyone on the folder where your .mdf , .ndf and log files staying.

A Troubleshooting Guide for Entity Framework , Locate Database Files dialog box will appear, browse through the path where MDF database file is located, click on file to select and then click on OK to exit. A new LDF file will be created by SQL Server while attaching the SQL database. The database will now appear in Databases

Comments
  • You say that you deleted the .mdf file. That is the SQL Server database file. If you've deleted it, there is no database, hence the error.
  • Wanted to say the same thing as @STLDeveloper , How can the code continue if it doesn't finds the required DB...
  • After compiling is there a OdeToFooDb.mdf in the bin\Debug\App_Data folder of your mvc project?
  • @rene No There is no mdf files at all. How do I generate an mdf file?
  • @rene There are no mdf file at all. I tried renaming the dbcontext class and now it works. What could have caused the issue?
  • I'd really like to know this fix using SSMS since I'm having the same problem and I'd like to keep the database name intact.
  • Yes please @Chris, how do you fix this problem with SSMS?
  • @julealgon that process explained here
  • Yo don't need to do that, only delete de database from (and close connections) from visual studio SQL Server Object Explorer View
  • I don't think that is a good way and absolutely unusable for production systems.
  • Removing the "Initial Catalog" portion from the connection string resolved this issue for me.
  • All the other suggestions were complete overkill. This was simple, terse and worked.
  • This has noting to do with the real problem. Of course you can specify a Catalog name.