James Ray Anderson

James Ray Anderson
James Ray Anderson
0 comments

How to Delete Entity Framework Migrations

3:27 PM
Overview
This post will provide information on how to purge or delete Entity Framework migrations.  Migrations, whether automatic or manual, may be stored in several places.  Database(s) and in-code scaffolding.

Step 1: Delete Migrations from Project
If you have run enable-migrations in your project you will likely find a "migrations" folder with one or more items within it.  If this folder exists, simply delete it.

Step 2: Clean Your Database
Using something like SQL Server Management Studio, locate your database and expand the tables.  Find the "_MigrationHistory" table and drop (delete) it.

Step 3: Verify Migration Deletion
In the Package Manager console, type the following:
get-migrations -verbose
Target database is: 'MyProject.MyContext' (DataSource: .\SQLEXPRESS, Provider: System.Data.SqlClient, Origin: Convention).
If you have migrations...you have another database to deal with!

The "Other" Database
When you specified the -verbose parameter the output should contain information on where the migrations are being stored, such as in your SQLEXPRESS pr LocalDB instance.  Even though the database you are ultimately targeting or working with is installed in another instance - Entity Framework will utilize SQLEXPRESS or LocalDb if it is available and you don't tell it otherwise.  This is controlled by the app.config of the project under the "defaultConnectionFactory".

Avoiding the "Other" Database Issue
You need to take control of where Entity Framework migrations go by doing one of the two following options:

Option #1
Specify the -ConnectionString or the -ConfigurationProviderName when adding a migration.

Option #2
Specify the connection string on the base of the context.
public MyContext() : base("myDatabase")


Want more resources?  Check out the Entity Framework Tutorial.  

0 comments:

 
Toggle Footer
Top