EntityFramework Core automatic migrations

Lapenkov Vladimir picture Lapenkov Vladimir · Sep 16, 2016 · Viewed 49.1k times · Source

Is there any code to perform automatic migration in Entity Framework core code first in asp.net core project?

I do it simply in MVC4/5 by adding

Database.SetInitializer(new MigrateDatabaseToLatestVersion<AppDbContext, MyProject.Migrations.Configuration>());
public Configuration() {
          AutomaticMigrationsEnabled = true;
        }

This saves time when entities changed

Answer

Frank Odoom picture Frank Odoom · Sep 16, 2016

You can call context.Database.Migrate()in your Startup.cs

eg:

using (var context = new MyContext(...))
{
    context.Database.Migrate();
}