21

I am running into the following error after updating EF to version 6.1.1:

An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll

Additional information: The model backing the TvstContext context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

We could fix this in the past as described in this question: Where is modelBuilder.IncludeMetadataInDatabase in EF CTP5?

However, I can't seem to get rid of the error.

Community
  • 1
  • 1
Tukurai
  • 213
  • 1
  • 2
  • 4
  • Did you enable the AutomaticMigrations? Or are you using manual migrations? If latter, did you try creating a new migration? – Johnny Jul 21 '14 at 09:05

3 Answers3

70

This should work, put it somewhere in the constructor of your derived DbContext:

Database.SetInitializer<MyContext>(null);
Dabblernl
  • 15,831
  • 18
  • 96
  • 148
  • Thanks. I find that EF gets model change tracking corrupted in some cases (e.g. lots of model and migration changes during refactoring for the best solution), and the only way to fix it is to manually roll back changes and delete the stuck migrations from migrationhistory table.This EF model change tracking can be really annoying sometimes. Laravel has migrations, but no model tracking, and it works just fine. I guess, that's the price for model tracking - additional complexity and issues when you want to fight against EF automatic tracking, so sometimes it's better to turn it off completely. – JustAMartin Apr 13 '16 at 14:25
  • 1
    Thanks. I dont need model changes tracking for my requirements. It just do the trick. – Miller Jun 01 '17 at 07:23
  • 1
    After far too many hours of this not working I found that I also had to update the EF config section in my web.config. I removed all settings from the context node and included disableDatabaseInitialization="true" on it. That's when it all started to work correctly. More info on that setting is at the bottom of https://msdn.microsoft.com/en-us/data/jj556606 – Brian Surowiec Sep 11 '17 at 07:09
  • this didn't work in the constructor. I just put it somewhere else in my code – Simon_Weaver Dec 04 '17 at 08:07
  • Will this still work with add-migration and update-database commands if I decide to write my own migrations as SQL raw commands but still using EF migration management tools ignoring the model consistency issues? – JustAMartin May 14 '19 at 11:51
24

You can also set it in Web.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>    
    <add key="DatabaseInitializerForType YourNamespace.YourDbContext, AssemblyName"
            value="Disabled" />
    </appSettings>
</configuration>

Decision from: http://www.entityframeworktutorial.net/code-first/turn-off-database-initialization-in-code-first.aspx

Sergei Shvets
  • 1,676
  • 1
  • 14
  • 12
  • 4
    This works and is arguably preferred, since no code changes are required and the compatibility check can be turned on/off depending on environment etc. – Jacek Gorgoń Sep 22 '16 at 15:20
3

To expand on Dabblernl's answers:

public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
        Database.SetInitializer<ApplicationDbContext>(null);
    }
PotatoJam
  • 319
  • 1
  • 4
  • 13