Reset EF code-first database in LocalDB

c# ef-code-first entity-framework localdb reset


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.

7/3/2012 9:02:35 AM

Accepted Answer

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
12/10/2013 2:09:09 AM

Popular Answer

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.

Related Questions


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