24

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?

3 Answers 3

22

In beta 7 and on, use:

using Microsoft.Data.Entity; ... context.Database.Migrate(); 
Sign up to request clarification or add additional context in comments.

8 Comments

This seems to be changed in beta7? looking for some info about how to migrate beta6 to beta7, ApplyMigrations isn't there anymore, ContextType attribute seems to have gone.. or my project just have gone haywire.. again. ;)
Start over - much have changed!
where do you put this code? Seems initializers and the like have changed so much I don't know where this would go.
In your application startup code, only to be run once
|
17

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(); 

3 Comments

Will context.Database.Migrate() creates a tables or else will create only Database?
Migration creates all schema elements, including tables.
For me its creating only Database, there is no tables in the Database. I have this line of code in startup.cs and I am running Add-Migration command
5

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>(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.