1

I installed a clean version of Windows 10 and Visual Studio 2015. I checked out my project from Visual Studio Team Services (was TFS Online) and built it. Ok! Then I went to run it and got this message when I hit a page that called the database.

enter image description here

I see the db was created by Entity Framework and my tables are there (when looking at SQL Server Object Explorer) in VS. But I'm a little confused as to why it's asking me to update the db, I haven't changed any thing since EF created it.

I then ran

update-database -verbose

in the console but I got this error message:

Cannot attach the file 'C:\TFS\YogaProject\YogaProject\App_Data\YogaProjectDatabase.mdf' as database 'YogaProjectDatabase'.

What else do I need to do here?

Here is my connection string

connectionString="Data Source=(localdb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\YogaProjectDatabase.mdf;Initial Catalog=YogaProjectDatabase;Integrated Security=True"

Esther Fan - MSFT
  • 8,091
  • 4
  • 26
  • 25
user1186050
  • 12,296
  • 22
  • 123
  • 274
  • Possible duplicate of [EF5: Cannot attach the file ‘{0}' as database '{1}'](http://stackoverflow.com/questions/13275054/ef5-cannot-attach-the-file-0-as-database-1) – Sandip Bantawa May 12 '16 at 05:57

2 Answers2

0

Solution 1

If you delete the DB file, it still stays registered with SqlLocalDB. Sometimes it fixes it by deleting DB. We can do this from the command line.

Open the Developer Command Propmpt for VisualStudio under your Start/Programs menu->All Programs->Visual Studio 2015->Visual Studio Tools

Run the following commands:

sqllocaldb.exe stop v11.0
sqllocaldb.exe delete v11.0

Now execute "update-database" command from package manager console and it will create database for you without any obstacles.

If you are using SQL LocalDb, don’t ever delete the physical .mdf and .log files for your database without going through the SQL Server Object Explorer in Visual Studio or in SQL Server Management Studio. If you delete the files only, you end up with an error like mentioned above.

Solution 2

May be your App_Data folder is empty? When you opened up the App_Data folder, you found an empty folder? It seems like you have to create the database file yourself. Opened up the solution, right clicked on the App_Data folder and choose Add –> New item->Create SQL Server Database… or get a file from the TFS -> Source Control Explorer

Denis Bubnov
  • 2,343
  • 4
  • 27
  • 50
0

I deleted the migrations entries from the migration table in Sql server. I followed this link Reset Entity-Framework Migrations

Community
  • 1
  • 1
user1186050
  • 12,296
  • 22
  • 123
  • 274