Reset EF code-first database in LocalDB

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

Question

For some code-first database prototype and testing, I'm utilizing Entity Framework 5.0 RC with the LocalDB database that comes pre-configured with Visual Studio 2012 RC.

I made a significant breaking change after a few iterations of updating my code-first database and the automated migration failed miserably. Since this is simply a prototype, I don't care whether the issue is resolved, therefore losing data is OK.

How can I clear the database or reset it? Reset must also reset the migration table, which Visual Studio's Server Explorer seems to have obscured.

1
6
7/3/2012 9:02:35 AM

Accepted Answer

In Visual Studio, drop the whole database there.

  1. the SQL Server Object Explorer window
  2. Locate the database you're using, then click and choose "Delete"
  3. Choose "Delete backup and restore history" and "Close existing connections" from the checkboxes, then click "OK."
  4. Rerun the software, and the database should be recreated.
8
12/10/2013 2:09:09 AM

Popular Answer

This is the finest strategy I have come across thus far.

  1. Create a non-system table for __MigrationHistory.
  2. Run a SQL script to delete every table.

See this piece for information on how to modify the __MigrationHistory table to support both new databases built using code-first and existing databases.

The SQL script looks like this:

EXEC sp_MSforeachtable @command1 = "DROP TABLE ?";

Because my Visual Studio 11 RC copy won't allow me to run queries on LocalDB for some reason, I'm using Free Database.NET v4 to execute this SQL script. Due to certain foreign keys, I also need to execute the script twice for it to delete all tables. Since it seems to be functioning adequately as is, I have chosen not to modify it.



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