concept Migrate method in category entity framework

appears as: Migrate method, The Migrate method, Migrate method
Entity Framework Core in Action

This is an excerpt from Manning's book Entity Framework Core in Action.

If you want to run your seed database method only when a new migration has been applied, you can use the DbContext method Database.GetPendingMigrations to get the list of migrations that are about to be applied. You must call GetPendingMigrations before you execute the Database.Migrate method, because the pending migration is empty after the Migrate method has finished.

Section 5.9.2 showed how to apply a migration on the startup of an ASP.NET Core application. You can apply a similar approach with other application types. The following listing shows an example for a console application, which executes the Migrate method every time it starts. If the database is up-to-date, the Migrate method does nothing.

Listing 11.4 Running the Migrate command during startup of a console application
class Program
{
    static void Main(string[] args)
    {
        using (var context = new EfCoreContext())    #1  
        {
            context.Database.Migrate();    #2  
        }
        //... then start the rest of my code
    }
}

#1   Setup of the appliction’s DbContext is done by overriding the OnConfiguring method, so you don’t need to provide any connection string of the database provider here.
#2   Calling the Migrate method applies any outstanding migrations to the database it’s attached to. If no outstanding migrations exist, it does nothing.

The advantage of using the Database.Migrate method is simplicity. This method ensures that any database that it connects to is migrated to the correct level before using it. This is the only approach that makes all migrations, breaking and nonbreaking changes (see section 11.5), in one stage. This works across all your databases, including the production database (with some caveats). But there are a few disadvantages:

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest