94

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?

1

5 Answers 5

111

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);
    }
6
  • 2
    Setting the initializer on the context's instance constructor doesn't make sense. EF will call the initializer before actually getting to that code if you try to make a new context.
    – Jcl
    Jul 11, 2016 at 17:03
  • 1
    Just verified the answer by looking at "Diagnostic Tool" in VS 2017. The ADO.NET calls before the first wanted SQL query have stopped after setting the intializer to null in the constructor.
    – Karl
    Mar 14, 2018 at 8:41
  • 3
    It works as expected. It's true EF will call the initializer before, but then calling it again inside the constructor makes the DbContext just ignore the migrations, in case you want to ignore the fact that your __MigrationHistory doesn't have the latest migration, and you don't want to do it anyway. In my case, I use migrations in dev environment, but when I deploy to production, I use SSDT to update the database. Therefore, EF would complain the model has changed because __MigrationHistory would not have the latest migration, but I can guarantee the database is updated. Mar 27, 2018 at 18:04
  • 1
    I would suggest moving the call to Database.SetInitializer from the constructor to the class constructor. That ensures the call is only made just once.
    – Steven
    Apr 14, 2020 at 12:13
  • @Steven Please explain what you mean by "moving the call to Database.SetInitializer from the constructor to the class constructor." What is the class constructor?
    – Polyfun
    Nov 9, 2022 at 9:50
46

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.
3
  • Unfortunately after following these steps, EF6 still checks for the existence of __MigrationHistory every time my application starts-up which adds a few more milliseconds to my app's startup time. Is there a way to disable the __MigrationHistory check entirely?
    – Dai
    Feb 4, 2020 at 1:47
  • @karlingen Please explain what you mean by "inside your DatabaseContext initializer."
    – Polyfun
    Nov 9, 2022 at 9:49
  • @Polyfun It was years ago I did anything related to this, but I think I meant the DatabaseContext's constructor. So put the logic inside the constructor of the DatabaseContext class. Basically what you are seeing in this answer: stackoverflow.com/a/29394811/1942551
    – karlingen
    Nov 9, 2022 at 21:29
29

If you want to completely turn off migrations:

https://stackoverflow.com/a/9709407/141172

However, I found it better to keep code first migrations enabled, but use the -Script option to have EF create a DB change script for me that I can apply to each database (development, QA, Production) manually:

Update-Database -Script -ProjectName MyProject -StartupProjectName MyProject

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

3
  • 1
    your link doesn't really corresponse to completely turn off migrations. To do that: add Database.SetInitializer<YourContextType>(null) to startup of your application Dec 14, 2015 at 16:28
  • Any idea how to achieve this with EF Core? Apr 11, 2020 at 19:59
  • @Shimmy: EF Core should never automatically try to alter your database. See github.com/dotnet/efcore/issues/3152
    – Eric J.
    Apr 11, 2020 at 20:03
26

If you already used Migrations then changing only Initializer won't help. You need to go to Management Studio, open your database tables, go to System Tables folder and remove __MigrationHistory table that is located there (for EF6 and above, it's located directly under Tables). This will disable Migrations for good.

3
  • 7
    The table __MigrationHistory is located directly under Tables, not System Tables. Oct 6, 2014 at 10:36
  • 6
    @PeterHedberg This is true for EF6+. For earlier versions it was under System Tables.
    – Episodex
    Mar 11, 2015 at 8:29
  • 2
    You can also rename the table, instead of deleting it, using sp_rename. I also disabled the initializer.
    – NMrt
    Jun 26, 2019 at 5:28
2

I just resolved this "issue" by

  1. Deleting table "_MigrationHistory" from the database.
  2. Deleting "Migrations" folder form the project.
  3. Updating EDMX file.
  4. Clean project & rebuild it.

The config of my environment is following

1. Visual Studio 2017 15.8.2
2. ASP NET MVC project
3. .NET Framework 4.6.1
4. Entity Framework 6.2.0
2
  • May i ask what you updated in the EDMX File? I am also using Database First and it is still querying the non existent Migration Tables... Thanks!
    – dalcam
    Nov 2, 2018 at 22:17
  • What if later we need migrations, does it creates new _MigrationHistory table automatically?
    – sairfan
    Feb 5, 2019 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.