Disable CodeFirst Migration for Entity Framework

Programming C#

There may be special circumstances when you completely want to get rid of EF auto Migration and control the database struture all by yourself (updates via SQL scripts, DBMS user interface and so on).

You can do so by

  1. Delete "Migrations" folder inside your project.
  2. Set Database.SetInitializer<DatabaseContext>(null); inside your DatabaseContext initializer.
  3. Delete the table __MigrationHistory inside your database. For EF6+ the table is located under Tables but for earlier versions it is located under System Tables.

DbContext Initializer

To completely disable Migrations use

public class DatabaseContext: DbContext
    //the base accepts the name of the connection string provided in the .config file as a parameter
    public DatabaseContext() : base("DatabaseContext")
        //disable initializer

Table __MigrationHistory

When you have several different Contexts that access the same Database (f.e: different areas with their own DbContext within a single website) and you want to disable Migration just for a single Context please don't delete the whole table.  Rather just remove the row for the Context in question from the table.


You could also keep automatic migration on and let EF create the update SQL script.  With this option you'll have full control on your live database and can even fine-tune that script and test it on your QA database before deploying to your live DB.  In the Package Manager Console create the update script like this:

PM> Update-Database -Script -ProjectName MyProject -StartupProjectName MyProject