EF Core(1.0.0)在Azure App Services上的迁移 [英] EF Core (1.0.0) Migrations On Azure App Services

查看:74
本文介绍了EF Core(1.0.0)在Azure App Services上的迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经将一个Web应用程序(Net Core 1.0.0-preview2-003121)部署到Azure应用服务,并且我们正在努力部署迁移.

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.

在RC1/2中,可以使用似乎自动存在的ef.cmd文件进行迁移,即我们可以使用该文件运行

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 

但是这个消失了.

dotnet ef本身未安装在Azure App Service中,因此这不是一个选择.

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

是否有不涉及从代码运行迁移或从Visual Studio部署迁移的想法?

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.

我的google fu显然使我失望,因为我一生无法找到任何东西,而且我不是唯一尝试在服务器上部署迁移的人

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

推荐答案

我们最终要做的是:

在构建方面,我们生成幂等数据库创建脚本:

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

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

其中ApplicationContext是EF上下文的名称,而migrations.sql是sql脚本文件的名称.

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

然后在部署方面,我们有一个小的powershell脚本,可以有效地运行migrations.sql脚本

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

这篇关于EF Core(1.0.0)在Azure App Services上的迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆