Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a migration in production

I am new to migrations and I can't find out what to do to get my changes onto the LiveDB.

So on dev when I add to my model I do

PM> add-migration <name>
PM> update-database

But what do I do on live? I was HOPING that I could just publish\deploy to live and the migration would run and update the schema, but I guess not :)

The Live SQL server is off in its own world I have no access to it from my dev box to just change the connectionstring and doing an update-database again.

What do you guys do, where's the docs?

Thanks, Steve

like image 669
Steve McNiven-Scott Avatar asked Sep 30 '16 17:09

Steve McNiven-Scott


3 Answers

Updated with sample for 3.0

The Core 3.0 approach is similar to 2.x, but now the generic host is used. You will need to add using Microsoft.Extensions.DependencyInjection; for the CreateScope()

public static void Main(string[] args)
{
    var host = CreateHostBuilder(args).Build();

    using (var scope = host.Services.CreateScope())
    {
        var db = scope.ServiceProvider.GetService<ShortenerContext>();
        db.Database.Migrate();
    }

    host.Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

Updated with better way for Core 2.0+

Migrations should be run in Program.cs due to tooling like the EF Core CLI tools running the Startup functions in normal execution.

Here is an example:

public class Program
{
  public static void Main(string[] args)
  {
      var host = BuildWebHost(args);

      using (var scope = host.Services.CreateScope())
      {
          var db = scope.ServiceProvider.GetService<ShortenerContext>();
          db.Database.Migrate();
      }

      host.Run();
  }

  public static IWebHost BuildWebHost(string[] args)
  {
      return WebHost.CreateDefaultBuilder(args)
          .UseStartup<Startup>()
          .Build();
  }
}

One way to run migrations in 1.x is to just add something like this in app startup:

public void Configure(
            IApplicationBuilder app,
            IHostingEnvironment env,
            ILoggerFactory loggerFactory,
            ShortenerContext db)
{
    db.Database.Migrate();
    //Rest omitted
}

This will execute all pending migrations against the database on startup.

like image 122
juunas Avatar answered Sep 19 '22 12:09

juunas


  1. Check the DB table "__EFMigrationsHistory" to figure out the last run migration
  2. In Visual Studio, pick Tools > NuGet Package Manager > Package Manager Console
  3. Run script-migration -From "last_migration_name" -To "current_migration_name" or script-migration -idempotent
  4. Visual Studio will open a new tab with the generated SQL script, run it on your DB
like image 28
Steve McNiven-Scott Avatar answered Sep 16 '22 12:09

Steve McNiven-Scott


In you Start-up method add this code

 using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
        {
            var context = serviceScope.ServiceProvider.GetService<AppDBContext>(); 
            context.Database.Migrate(); 
        }

This will execute and update all migrations on production

like image 40
Sayed Muhammad Idrees Avatar answered Sep 20 '22 12:09

Sayed Muhammad Idrees