Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable code first migrations

I have a code-first entity model in EF5. But I want to manage the database changes manually -- I do not want EF to modify my existing database and all its data. But when I make parallel changes in the EF mapping and in the database, EF refuses to operate properly telling me I need to use code first migration. How do I turn this off?

like image 494
Stan Hargrove Avatar asked Feb 01 '13 19:02

Stan Hargrove


People also ask

How do I get rid of migrations?

Delete your Migrations folder. Create a new migration and generate a SQL script for it. In your database, delete all rows from the migrations history table. Insert a single row into the migrations history, to record that the first migration has already been applied, since your tables are already there.

Can I use EF core without migration?

Hi @PanagiotisKanavos, as you mentioned, it does not require migration.

How do I get rid of dotnet migrations?

Delete the row corresponding to your migration that you want to unapply (Say "yes" to the warning, if prompted). Run "dotnet ef migrations remove" again in the command window in the directory that has the project. json file. Alternatively, run "Remove-Migration" command in the package manager console.

What is migration in code first approach?

Code First Migrations is the recommended way to evolve your application's database schema if you are using the Code First workflow. Migrations provide a set of tools that allow: Create an initial database that works with your EF model. Generating migrations to keep track of changes you make to your EF model.


2 Answers

set the Database.SetInitializer to null.

public class DatabaseContext: DbContext {     //the base accepts the name of the connection string provided in the web.config as a parameter     public DatabaseContext()         : base("DatabaseContext")     {         //disable initializer         Database.SetInitializer<DatabaseContext>(null);     } 
like image 78
Sarath Rachuri Avatar answered Sep 24 '22 19:09

Sarath Rachuri


So the most complete answer that I have found is this:

  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.
  4. Build and run.
  5. Profit.
like image 43
karlingen Avatar answered Sep 24 '22 19:09

karlingen