5

We have a web app (Net Core 1.0.0-preview2-003121) deployed to an Azure App Service and we're struggling to deploy the migrations.

In RC1/2 it was possible to do the migrations with an ef.cmd file that seemed to be automagically there, namely we could use this file to run

dnx ef database update 

but this is gone.

dotnet ef is not installed in the Azure App Service itself, so this is not an option.

Any ideas that don't involve running the migrations from code/deploying them from visual studio?

We're trying to build a continuous deployment pipeline and I'd rather avoid forcing the deployment of migrations from code.

MY google fu is clearly failing me here as it can't for the life of me find anything and i can't be the only one trying to deploy the migrations on the server

TIA

  • The CLI has changed. https://docs.efproject.net/en/latest/miscellaneous/cli/dotnet.html – Benjamin Abt Sep 21 '16 at 20:08
  • @Ben it has indeed. I alluded to that fact on the post by mentioning that dotnet ef is not installed, probably not clear that i meant on the azure app service itself and not my machine – ManyRootsofAllEvil Sep 23 '16 at 04:57

2 Answers2

2

What we ended up doing is:

On the build side we generate an idempotent database creation script:

dotnet ef migrations script --idempotent --output migrations.sql  --context  ApplicationContext

Where ApplicationContext is the name of your EF context and migrations.sql is the sql script file name.

Then on deployment side we have a small powershell script that effectively runs the migrations.sql script

param(
[Parameter(Mandatory)]
[string]$server,
[Parameter(Mandatory)]
[string]$dbname,
[Parameter(Mandatory)]
[string]$dbadmin,
[Parameter(Mandatory)]
[string]$dbpassword,
[Parameter(Mandatory)]
[string]$migrationPath
)

function Deploy-Migrations ($migrationPath,$DBSettings)
{
   #Setting up database connection
   $connection = New-Object System.Data.SqlClient.SqlConnection
   $connection.ConnectionString = [string]::Format("Data Source=tcp:{0}.database.windows.net,1433;Initial Catalog={1};User Id={2}@{0};Password={3};MultipleActiveResultSets=True", $DBsettings['sqlServerName'], $DBsettings['databasename'],$DBsettings['adminAccount'], $DBsettings['adminPassword']) 

    try
    {
        $connection.Open();

         $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
         $SqlCmd.Connection = $connection
         $query = Get-Content $migrationPath
         $sqlCmd.CommandText = $query.Replace("GO","") # This is required to prevent "syntax" complaints
         $sqlCmd.ExecuteNonQuery()

         Write-Host "Migration Deployed" 
    }
    Catch
    {

        Write-Error "oops ... PAnic ... $($_.Exception.Message) on $($_.Exception.ItemName)"
        break
    }
    Finally
    {
        $connection.Close()
    }  
 }

$DBSettings = @{"sqlServerName"=$server; "databasename"=$dbname;    "adminAccount"=$dbadmin; "adminPassword"=$dbpassword }

Deploy-Migrations $migrationPath $DBSettings
  • 4
    Thanks for this. Sometimes working with ASP.NET Core makes me feel like I'm the only person in the world who has ever used it. O_O SQL Scripts it is .... – John Hargrove Feb 24 '17 at 05:09
0

As of .Net 6.0 (and perhaps .Net Core 3.x)...

You can have a deployScript in an ARM template perform the migration for you. Here is an example that was designed as an Azure Marketplace deployment. That example assumes that you've pre-generated a script.sql that is kept in an artifacts folder of the marketplace app deployment "bundle". The deployScript then simply runs an Invoke-Sqlcmd to run the script.sql file.

Brent Arias
  • 29,277
  • 40
  • 133
  • 234