Archive for the ‘.NET’ Category

EF Core - Burning the Database to the Ground

Friday, February 1st, 2019

During development, you sometimes want to make database changes that a migration cannot handle.  One such example is changing the datatype of a primary key column from an int to bigint.  In such a case you may want to burn the database to the ground and have a single migration that creates all the database structures defined through the model to date.  Some developers routinely remove all migrations to “cleanup” the code.

The procedure to do so is different for EF vs EF Core, and if you don’t do it the right way, things can get screwed up and be hard to fix.  With EF Core the easiest way I have found to burn the database to the ground is to use the PMC command:

update-database 0

This is a little different than simply removing all the database tables from a SQL utility like SMS.  It actually removes all tables, but ensures that the __EFMigrationsHistory table is present with 0 rows.

Then you can create delete all the files in the Migrations folder of the C# project and create a new initial migration:

add-migration "Some Title"

and generate the new structures:

update-database