74

I'm trying to ignore the "Automatic" migration using Entity Framework 6.0 rc1. My problem is that I don't want this feature right now and every time that my application runs I can see all entity logs trying to create all tables.

Anticipate thanks.

SeyedPooya Soofbaf
  • 2,654
  • 2
  • 29
  • 31
Michel Borges
  • 921
  • 1
  • 9
  • 16
  • 11
    Have you tried disabling the database initializer by using `Database.SetInitializer(null)`? – Pawel Sep 06 '13 at 23:28
  • 3
    Late note, but the title would be more accurate if you said "disable automatic migration", as opposed to code-based migrations – Savage Aug 16 '16 at 12:53

7 Answers7

51

You can put this inside your entityFramework section of the app.config:

<contexts>
  <context type="YourNamespace.YourDbContext, YourAssemblyName" disableDatabaseInitialization="true"/>
</contexts>

This msdn page tells all about the Entity Framework Configuration Section.

Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
Carlos Teixeira
  • 714
  • 7
  • 12
  • 1
    This is a much better way to permanently disable the database initialization – Tom Halladay Apr 26 '15 at 05:56
  • 5
    This page tells all about the Entity Framework Configuration Section: https://msdn.microsoft.com/en-us/data/jj556606 – KevinVictor Jul 27 '15 at 21:25
  • 1
    what is `app.config`, do you mean `web.config` ? – Muflix Oct 17 '16 at 08:22
  • 1
    how I determine what is `YourAssemblyName` ? – Muflix Oct 17 '16 at 08:25
  • where in the configuration put this code ? into `` section ? – Muflix Oct 17 '16 at 08:26
  • 1
    App.config or Web.config, whatever you are using. 'YourAssemblyName' is the name of the assembly where you have your DbContext. You can right-click on the project and check it under the 'Application' tab. You have to put it inside your 'entityFramework' section, under 'configuration'. – Carlos Teixeira Oct 19 '16 at 10:42
  • 1
    To disable the initialization, you have to set disableDatabaseInitialization to true, not false. – Carlos Teixeira Jan 31 '18 at 15:26
  • In a solution I have model project and web project (set as startup project). My EF is installed on the model project... but I have entityframework section on both App.config (model project) or Web.config (web project)... why? is that correct? – Fernando Torres Jan 31 '19 at 20:30
50

Try this:

internal sealed class Configuration : DbMigrationsConfiguration<YourContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }
}

UPDATE:

You can also try this:

Database.SetInitializer<YourContextType>(new CreateDatabaseIfNotExists());
SeyedPooya Soofbaf
  • 2,654
  • 2
  • 29
  • 31
  • 2
    Yes, now it's working. I guess I was doing something wrong. The problem was that I deleted my __migratioHistory table and all migration files. I did Database.SetInitializer(new NullDatabaseInitializer()); the same as Database.SetInitializer(null) thanks guys – Michel Borges Sep 09 '13 at 16:07
  • 2
    Guys, I guess I was not clear in my question. I realize that my problem was that I have an abstract base class that inherits from DbContext, called DefaultDbContext. The problem was that I did Database.SetInitializer(null) but the correct way is Database.SetInitializer(null). Thanks! – Michel Borges Sep 14 '13 at 00:03
  • The first answer seems to work for me. I'm using a case where I make small models with Code First for an existing DB that I'll never change. Thanks. – Wade Hatler Apr 23 '14 at 18:49
  • 1
    As soon as you have enabled the Migrations (added the Configuration class) both of this solution will not prevent running the Migrations. AutomaticMigrationsEnabled = false - disables the automatic migration database WITH NO MIGRATIONS IN THE CODE. Moreover my expectation was to avoid the migration running with the CreateDatabaseIfNotExists initializer. I expected that EF will create a database based on the model without the Migrations. But both CreateDatabaseIfNotExists and MigrateDatabaseToLatestVersion act the same way in case existing the migrations. – SerjG Sep 05 '16 at 16:35
  • Please take a look at the blog: https://msdn.microsoft.com/en-us/en-en/magazine/dn818489.aspx – SerjG Sep 05 '16 at 17:44
  • I placed the Database.SetInitializer(new CreateDatabaseIfNotExists()); in an static constructor to ensure calling only once and to not be exposed outside my DLL – dariogriffo Apr 18 '18 at 14:57
30

Via web.config see - https://msdn.microsoft.com/en-us/data/jj556606.aspx#Initializers

Via Code (oddly, MUCH simpler answer)

public class MyDB : DbContext
{
    public MyDB()
    {
        Database.SetInitializer<MyDB>(null);
    }
}

or in Global.asax.cs

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        // ...

        Database.SetInitializer<MyDB>(null);

        /// ...

    }
}
Aaron Sherman
  • 3,789
  • 1
  • 30
  • 33
  • 2
    For Code First EF, this is the cleanest answer. – Brain2000 Jun 16 '16 at 18:53
  • 2
    Don't put it in the constructor for the `DbContext` – Issa Fram Sep 10 '16 at 03:22
  • 4
    Where would you put it? Why not in the constructor? (real question, not sarcasm) – Aaron Sherman Sep 11 '16 at 19:04
  • What is `Database` namespace ? – Muflix Oct 17 '16 at 08:21
  • 1
    @AaronSherman instead of putting this in the constructor, it can be called at any point before creating `MyDB`, e.g. in `Application_Start()`. It's a static method used to configure the initializer used by Entity Framework, so it should be called before the `DbContext` is created. – DigitalDan Jan 21 '17 at 16:43
  • 1
    @Muflix `Database` is in the `System.Data.Entity` namespace. – DigitalDan Jan 21 '17 at 16:44
  • 1
    Nulling the dbase initializer in the entity class constructor results in a much faster initial view generation as compared to setting the initializer to null after the db context is instantiated. In my app, I had 1-1/2 MINUTE delay in my first db query. Found / fixed a couple of independent associations and got down to 55 seconds. Setting the migrations initializer after instantiating the db context got the first call down to 15 seconds. Moving the nulling of the initializer to the entity class constructor reduced the first call delay to just a couple of seconds... voila! – IdahoB Apr 14 '17 at 17:04
  • 1
    @IdahoB: I realize I'm a couple years late to the party.. but, you mention nulling the initializer in the entity class ctor is faster than nulling it after the dbcontext is instantiated. Clarification please: Are you talking about nulling the initializer in the POCO ctor? Or in the DbContext ctor? – Sam Axe Jun 20 '17 at 22:45
  • 4
    Personally I find adding the `SetInitializer` to the `static constructor` of my DbContext classes the cleanest approach. – Michal Ciechan Feb 18 '20 at 23:41
4

If you found this question hoping for a simple answer to disable migrations because you typed "Enable-Migrations" and now things aren't working the way you expected, like not running the seed method you thought it would run, then look in the solution explorer and delete the Migrations folder. That will stop the code from looking at the migrations config to find initialization code. To get the Migrations folder back, just run "Enable-Migrations" again.

javovo
  • 317
  • 2
  • 7
3

The mistake I was making was to call Database.SetInitializer(null); too late (after the context had been initialised). The best way to ensure migrations are disabled is to make the above call for all your contexts in your application startup. I favor this approach over setting it in the app.config so I can use my container to locate my contexts and then construct a call.

var migrationsMethod = typeof(System.Data.Entity.Database).GetMethod("SetInitializer");
foreach (var contextType in allContextTypes)
{
    migrationsMethod.MakeGenericMethod(contextType).Invoke(null, new object[] { null });                            
}
user663470
  • 149
  • 7
1

Disabling the automatic migration can also be configured during the invoke of the enable-migrations command (which creates the Configuration class), using the EnableAutomaticMigration parameter with a value of false:

enable-migrations -EnableAutomaticMigration:$false -ContextTypeName FullyQualifiedContextName

The will create a Configuration class which sets the AutomaticMigrationsEnabled property to false, like in the answer above.


The EnableAutomaticMigration parameter of the enable-migrations command is mentioned in this article of the Entity Framework Tutorial page (however they use it with true which seems to be the default value).

Martin
  • 5,165
  • 1
  • 37
  • 50
1

Try this, Add this line in your MyContext class, this will be called before your MyContext constructor is called. This will stop creating the database as well as won't add tables into a connected database. Basically this line disables the default Code-First Database Initialization strategy which basically has a default strategy as CreateDatabaseIfNotExists.

static MyContext()
{
       System.Data.Entity.Database.SetInitializer<MyContext>(null);
}
xyzWty
  • 133
  • 1
  • 14