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,276
  • 4
  • 27
  • 25
chuckd
  • 13,460
  • 29
  • 152
  • 331
  • 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,619
  • 5
  • 30
  • 54
  • I get a message that says 'The specified LocalDB does not exist" – chuckd May 12 '16 at 05:55
  • comment to your solution 2: I don't have anything in App_Data and never did when using Win 8.1 and VS 2013. – chuckd May 12 '16 at 05:57
  • @user1186050, add your connection string from config in your question, please – Denis Bubnov May 12 '16 at 05:58
  • added my connection string above – chuckd May 12 '16 at 05:59
  • @user1186050, you don't have file in `C:\TFS\YogaProject\YogaProject\App_Data\YogaProjectDatabase.mdf` for migration you need the database. – Denis Bubnov May 12 '16 at 06:00
  • maybe the file YogaProjectDatabase.mdf is in readonly since it was check in to TFS? – Dr. Stitch May 12 '16 at 06:03
  • the .mdf file is in App_Data. I just looked. Nor is it read only – chuckd May 12 '16 at 06:04
  • is this the correct command for VS 2015's Sql Express? sqllocaldb.exe stop v11.0 seems like this might be for an eearlier version – chuckd May 12 '16 at 06:05
  • ya I need to use MSSQLLocalDB – chuckd May 12 '16 at 06:12
  • ok, I deleted the db ran through the register page and still received the first error message about needing update the database. then I updated it and received a message saying that there is already an object named YogaProfile in the database! Why would it be trying to create another profile object (table) if the EF built all the tables when I tried to register? – chuckd May 12 '16 at 06:17
  • @user1186050, I checked v11.0 - is correct for VS2015. For MSSQLLocalDB...need to think – Denis Bubnov May 12 '16 at 06:17
0

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

Community
  • 1
  • 1
chuckd
  • 13,460
  • 29
  • 152
  • 331