I am using Entity Framework Code First approach with AutomaticMigrationsEnabled = true
:
Database.SetInitializer(new MigrateDatabaseToLatestVersi
Automatic Migrations means that you don't need to run add-migration
command for your changes in the models, but you have to run update-database
command manually.
If Automatic Migrations is enabled when you call update-database
, if there are pending changes in your models, an 'automatic' migration will be added and database will be updated.
If you want that your database is updated without need to call update-database
command, you can add Database.SetInitializer(...)
in OnModelCreating()
method on your context, like so:
public class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion());
}
...
}
public class MigrateDBConfiguration : System.Data.Entity.Migrations.DbMigrationsConfiguration
{
...
Note that you should declare DbMigrationsConfiguration
and MigrateDatabaseToLatestVersion
with your real context, not the default DbContext
.