To enable automatic migrations you need to add this to your configuration :
public class MigrateDBConfiguration : System.Data.Entity.Migrations.DbMigrationsConfiguration<DbContext>
{
public MigrateDBConfiguration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
}
To enable automatic updates to your database you need to add Database.SetInitializer(...) in OnModelCreating() method on your context :
public class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, MigrateDBConfiguration>());
}
...
}
You can also use Fluent migration to make the database updates and migrations automatic.
Here's an example on how to use it from Here:
using FluentMigrator;
namespace DatabaseMigration.Migrations
{
[Migration(1)]
public class M0001_CreateMemberTable:Migration
{
public override void Up()
{
Create.Table("Member")
.WithColumn("MemberId").AsInt32().PrimaryKey().Identity()
.WithColumn("Name").AsString(50)
.WithColumn("Address").AsString()
.WithColumn("MobileNo").AsString(10);
}
public override void Down()
{
Delete.Table("Member");
}
}
}