Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset EF code-first database in LocalDB

I am using Entity Framework 5.0 RC with the LocalDB database that comes pre-configured with VS 2012 RC for some prototyping and testing of code-first database.

After a few cycles of changing my code-first database and running "update-database" I did a huge breaking change and automatic migration failed bad. I don't care about resolving this conflict since this is just prototyping, so losing data is fine.

How do I reset or delete the database? Reset must also reset the migration table, which seems to be hidden from Server Explorer in Visual Studio.

like image 230
angularsen Avatar asked Jul 03 '12 09:07

angularsen


People also ask

How do I clear my LocalDB database?

In order to be deleted, first it should be stopped. Type the SqlLocalDB stop MSSQLLocaDB command in the Command Prompt window: LocalDB instance “MSSQLLocalDB” stopped. Now, repeat the SqlLocalDB delete MSSQLLocalDB command.

How do I update my Entity Framework database first?

Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish.


2 Answers

Drop the whole database in Visual Studio

  1. Open SQL Server Object Explorer
  2. Find the database you are working with Right click and select delete
  3. Select the two check boxes (Delete backup and restore history, and Close existing connections) and click ok
  4. Run program again and it should recreate the dabase
like image 149
Nico Avatar answered Sep 21 '22 08:09

Nico


So far this is the best approach I have found.

  1. Make __MigrationHistory a non-system table.
  2. Execute SQL script to drop all tables.

To alter __MigrationHistory table, see this article to handle both existing databases and new databases created with code-first.

The SQL script is as follows:

EXEC sp_MSforeachtable @command1 = "DROP TABLE ?";

I am using Database .NET v4 Free to run this SQL script, because my Visual Studio 11 RC copy won't let me run queries on LocalDB for some reason. Also, I need to run the script twice for it to drop all tables due to some foreign keys. I haven't bothered fixing this since it seems to work well enough as it is.

like image 29
angularsen Avatar answered Sep 19 '22 08:09

angularsen