Disabling Entityframework Migrations

I have found a couple ways migrations can be turned off. The version of Entityframework you are using may determine which method you use.

The first method is to set the AutomaticMigrationsEnabled property to false in the DBMigrationsConfiguration class.

using System.Data.Entity.Migrations;
 
namespace DemoApplication.Migrations
{
    internal sealed class Configuration : DbMigrationsConfiguration<MyDBContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }
 
        protected override void Seed(MyDBContext context)
        {
            //  This method will be called after migrating to the latest version.
 
            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }
}

The second method is to set a null Database Initializer in the constructor of the DBContext class. I have used this method in projects using Entityframework 6 where DBMigrationsConfiguration class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace DemoApplication.Models.Concrete
{
    public class EFDbContext : DbContext
    {
        //the base contains the name of the connection string provided in the web.config
        // this constructor should disable migrations
        public EFDbContext()
            : base("EFDbContext")
        {
            //disable initializer
            Database.SetInitializer<EFDbContext>(null);
        }

        public DbSet<Query> Query { get; set; }
    }
}

In any case, if there is a _MigrationHistory table in the database then delete it. This will be a user table in EF6. I believe it will show up as a system table in earlier versions of Entityframework.

As a last note. Some people have reported to have used disable-migrations in the Package Manager Console window. However when I have tried this in the past on EF6 it has not worked.

PM> disable-migrations
The term ‘disable-migrations’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
At line:1 char:19
+ disable-migrations <<<<
+ CategoryInfo : ObjectNotFound: (disable-migrations:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

Leave a Reply