How can I disable migration in Entity Framework 6.0

Entity FrameworkEntityEntity Framework-Migrations

Entity Framework Problem Overview


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.

Entity Framework Solutions


Solution 1 - Entity Framework

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.

Solution 2 - Entity Framework

Try this:

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

UPDATE:

You can also try this:

Database.SetInitializer<YourContextType>(new CreateDatabaseIfNotExists());

Solution 3 - Entity Framework

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);
       
        /// ...

    }
}

Solution 4 - Entity Framework

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.

Solution 5 - Entity Framework

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 });                    	    
}

Solution 6 - Entity Framework

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).

Solution 7 - Entity Framework

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);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichel BorgesView Question on Stackoverflow
Solution 1 - Entity FrameworkCarlos TeixeiraView Answer on Stackoverflow
Solution 2 - Entity FrameworkSeyedPooya SoofbafView Answer on Stackoverflow
Solution 3 - Entity FrameworkAaron ShermanView Answer on Stackoverflow
Solution 4 - Entity FrameworkjavovoView Answer on Stackoverflow
Solution 5 - Entity Frameworkuser663470View Answer on Stackoverflow
Solution 6 - Entity FrameworkMartinView Answer on Stackoverflow
Solution 7 - Entity FrameworkxyzWtyView Answer on Stackoverflow