In a previous post I explained how to recover after deleting a Entity Framework database. In this post we’ll see the proper way to recreate the database in Entity Framework:
In Visual Studio:
- Go to Server Explorer, right-click on the data collection that represents your context and choose delete.
- Go to Solution Explorer and delete the .mdf file. You might have to click on the “Show All Files” icon before you see it.
- Go to the Solution Explorer and delete the
At this point, you have a solution that will create a new database when its run. If you need to seed the database or expect your models to change, then you’ll want to do the following:
- Tell EF to create a fresh database bases on the current models by going to the Package Manager Console and running the following commands:
Enable-Migrations Add-Migration Initial Update-Database
- Update the code in
Migrations\Configuration.csto seed the database with the data you need.