Reset EF code database in LocalDB

I am using Entity Framework 5.0 RC with a LocalDB database that is preconfigured using VS 2012 RC for some prototypes and testing a code-based database.

After several cycles of changing my first code base and starting the โ€œupdate databaseโ€, I made huge changes and the automatic migration failed. I don't care about resolving this conflict, as it is just prototyping, so data loss is fine.

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

+6
source share
3 answers

Drop the entire database in Visual Studio

  • Open SQL Server Object Explorer
  • Find the database you are working with. Right click and select delete
  • Select two checkboxes (Delete backup and recovery history and Close existing connections) and click ok
  • Run the program again and it should recreate the dabase
+8
source

So far this is the best approach I have found.

  • Make __MigrationHistory a non-system table.
  • Run SQL script to delete all tables.

To modify the __MigrationHistory table, see this article for processing both existing databases and new databases created using code.

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 copy of Visual Studio 11 RC will not allow me to run queries in LocalDB for some reason. In addition, I need to run the script twice so that it deletes all tables due to some foreign keys. I was not worried about this, as it seems to work quite well as it is.

+1
source

I encountered the same problems, but I found a way to see the __MigrationHistory table for LocalDB istance, instead of using Server Explorer, you should try SQL Server Object Explorer , you will find the table in the System Tables of the created database.

You can directly output data using the command to present data in a table, but you can query it without any problems.

 select * from __MigrationHistory 

This way, you can leave it as the article you reported about for existing migrations.

0
source

Source: https://habr.com/ru/post/919561/


All Articles