Explore Courses Blog Tutorials Interview Questions
+2 votes
in Azure by (20.3k points)

if (dev.isBored() || {

searchjobs({flexibleHours: true, companyCulture: 100});


// A carrer site that's by developers, for developers.

I have got into a bad state with my ASP.Net MVC 5 project, using Code-First Entity Framework. I don't care about losing data, I just want to be able to start fresh, recreate the database and start using Code-First migrations.

Currently I am in a state where every attempt to Update-Database results in an exception being thrown or getting an error message. Also the website can't access the database correctly. How can I wipe all migrations, re-create the database and start from scratch without having to create a new project? In other words, I want to keep my code but drop the database.

Later I will also want to get the deployment database (SQL Server on Azure) in sync. Again, I don't mind dropping all the data - I just want to get it working.

Please provide any how-to steps to get back to a clean state. Much appreciated.

1 Answer

+2 votes
by (47.2k points)
edited by

1) In Visual Studio, go to Server Explorer and check if the ".mdf" data Connections for this project are connected, if so, right click and delete.

2 )In Solution Explorer, click Show All Files icon.

3) Select App_Data, right click and delete all ".mdf" files for this project.

4) Delete Migrations folder 

5) Open SQL Server Management Studio, make sure the database for this project is not there, otherwise delete it.

6) now go to the Package Manager Console in Visual Studio and type:

Enable-Migrations -Force

Add-Migration init


7) Run your application

Note: if you get an error, in step 6 part 3 "Cannot attach the file...", it is possibly because you didn't delete the database files completely in SQL Server.

Browse Categories