20

Here is some working EF6 migration code:

Database.SetInitializer<CmContext>(null);
var settings = new MigrationsConfiguration();
var migrator = new DbMigrator(settings);
migrator.Update();

What is the equivalent using EF Core?

MarredCheese
  • 17,541
  • 8
  • 92
  • 91
Dmitry L
  • 211
  • 1
  • 2
  • 4

3 Answers3

20

In beta 7 and on, use:

using Microsoft.Data.Entity;

...

context.Database.Migrate();
ErikEJ
  • 40,951
  • 5
  • 75
  • 115
16

For Entity Framework Core 1.0.0, ensure you have the Microsoft.EntityFrameworkCore.Relational NuGet package. Then import this namespace:

using Microsoft.EntityFrameworkCore;

Finally, get hold of a DbContext and run:

context.Database.Migrate();
Edward Brey
  • 40,302
  • 20
  • 199
  • 253
2

According to the Microsoft documentation, for more advanced scenarios than simply applying the migrations which are already present in your project file structure, you can use the EF Core IMigrator service. You can easily access the internal implementation by utilizing the following access code:

var migrator = myDbContext.GetInfrastructure().GetService<IMigrator>();