How to re-create database for Entity Framework?

asp.net-mvc azure azure-sql-database entity-framework

Question

With my ASP.Net MVC 5 project that uses the Code-First Entity Framework, I have gotten into a horrible situation. I don't care if data is lost; all I want is to be able to build a new database and begin using Code-First migrations.

At the moment, if I try to update the database, an exception is raised or I receive an error message. Additionally, the website cannot effectively access the database. Without starting a new project, how can I delete all migrations, recreate the database, and start over? I want to preserve my code but get rid of the database, in other words.

Later, I'll also want to synchronize the SQL Server on Azure deployment database. Once more, I don't mind losing all the data; all I want is for it to function.

Please outline any instructions for returning to a clean state. Thank you very much.

1
142
11/1/2018 5:17:42 AM

Accepted Answer

Follow the instructions below:

1) Open Server Explorer in Visual Studio and see if the ".mdf" Data Connections for this project are active; if they are, delete them by right-clicking.

2) Open Solution Explorer and select the icon for Show All Files.

3) Navigate to App Data, right-click all the ".mdf" files for this project, and then remove them.

4) Right-click the Migrations folder and select "Delete."

5) Open SQL Server Management Studio and check to see if the project's database is present; if it is, delete it.

6) In Visual Studio, go to the Package Manager Console and type:

  1. Enable-Migrations -Force
  2. Add-Migration init
  3. Update-Database

7) Launch your program

Note: If you get the message "Cannot attach the file..." in step 6 part 3, it may be because you didn't entirely remove the database files from SQL Server.

209
1/19/2016 1:59:46 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow