How to disable the first code migrations

EF5 has the first code entity model. But I want to manage database changes manually - I don't want EF to modify my existing database and all its data. But when I make parallel changes to the EF mapping and the database, EF refuses to work properly, telling me that I need to use the first code migration. How to disable this?

+78
migration entity-framework code-first
Feb 01 '13 at 19:59
source share
5 answers

set 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); } 
+91
Apr 01 '15 at 15:26
source share

So, the most complete answer I found is this:

  • Delete Migrations folder inside your project.
  • Set Database.SetInitializer<DatabaseContext>(null); inside the DatabaseContext initializer.
  • Delete the __MigrationHistory table inside your database. For EF6 +, the table is in Tables , but for earlier versions, it is under System Tables .
  • Build and run.
  • Profit
+37
Nov 11 '16 at 8:06
source share

If you want to completely disable migration:

stack overflow

However, I was better off preserving the inclusion of the first code migrations, but use the -Script option -Script that EF creates a script database change for me, which I can apply to each database (development, QA, Production) manually

 Update-Database -Script -ProjectName MyProject -StartupProjectName MyProject 

That way, EF will create a script change for me, and I still have full control over the changes applied. I use change scripts like any other source code.

+25
Feb 01 '13 at 20:02
source share

If you have already used Migrations, then changing only the Initializer will not help. You need to go to Management Studio, open the database tables, go to the System Tables folder and delete the __MigrationHistory table that is there (for EF6 and above, it is located directly under Tables ). This will disable migration permanently.

+22
Aug 08 '13 at 9:13
source share

I just solved this problem

  1. Removing table "_MigrationHistory" from the database.
  2. Removing a project from the Migrations folder.
  3. Updating the EDMX file.
  4. Clean the project and rebuild it.

My environment

 1. Visual Studio 2017 15.8.2 2. ASP NET MVC project 3. .NET Framework 4.6.1 4. Entity Framework 6.2.0 
0
Sep 10 '18 at 2:59
source share



All Articles