我正在使用PowerShell来应用EF迁移。它正在运行,唯一的问题是,如果在应用迁移时出现任何错误,它仍然会成功。我希望能够在应用迁移时出现任何错误时引发异常。无法找到有关如何执行此操作的任何信息。这是我的脚本:

  try
 {
#all relative paths are relative to where xyzabc.sln is

#copy migrate.exe to xyzabcWebRole\bin
Copy-Item packages\EntityFramework*\tools\migrate.exe xyzabc\path\xyzabcWebRole\bin -ErrorAction "Stop"

#migrate
$migrator = "C:\TeamCity\buildAgent\work\f791999b69d7a83e\packages\EntityFramework.6.1.3\tools\migrate.exe"
$migrateCommand = "$migrator xyzabcWebRole.dll /StartUpDirectory=C:\TeamCity\buildAgent\work\f791999b69d7a83e\xyzabcWebRole\bin /connectionStringName:MyDBConnectionString /startUpConfigurationFile:C:\TeamCity\buildAgent\work\f791999b69d7a83e\xyzabc\path\xyzabcWebRole\Web.config /verbose"

Write-Host $migrateCommand
Invoke-Expression $migrateCommand


}
  catch [System.Exception]
{
Write-Host $_.Exception.ToString()
exit 1
}

最佳答案

调用迁移并引发异常后,检查$LASTEXITCODE变量

08-19 04:00