【问题标题】:dotnet ef migrations script fails in CI but works fine locallydotnet ef 迁移脚本在 CI 中失败,但在本地工作正常
【发布时间】:2021-10-26 14:43:21
【问题描述】:

问题背景:

我有一个包含数据库迁移的类库项目 (MyProject.MigrationProject.csproj)。而在入口项目(Web API)的startup.cs中,我已经明确地包含了如下迁移程序集。

 services.AddDbContext<ApplicationDbContext>(options =>
   options.UseSqlServer(
      configuration.GetConnectionString("DefaultConnection"),
      x => x.MigrationsAssembly("MyProject.MigrationProject")));

然后我使用 powershell 在本地运行 dotnet ef migration 命令。我正在使用的命令是:

dotnet ef migrations script --no-build -o D:\migrations\script.sql --idempotent --project D:\...\src\MyProject.MigrationProject\MyProject.MigrationProject.csproj --startup-project D:\...\src\MyProject.WebApi\MyProject.WebApi.csproj

上述命令在我的机器上成功执行,并在输出位置创建了所需的 script.sql 文件。然后在 Azure Devops 的构建管道(使用命令行任务)中使用相同的命令,但由于某种原因它在那里失败。 Devops 上的命令如下所示:

dotnet ef migrations script --no-build -o $(Build.ArtifactStagingDirectory)\migrations\script.sql --idempotent --project $(Build.SourcesDirectory)\src\MyProject.MigrationProject\MyProject.MigrationProject.csproj --startup-project $(Build.SourcesDirectory)\src\MyProject.WebApi\MyProject.WebApi.csproj

错误我从 Devops 得到的:

Script contents:
dotnet ef migrations script --no-build -o D:\a\1\a\migrations\script.sql --idempotent --project D:\a\1\s\src\MyProject.MigrationProject\MyProject.MigrationProject.csproj --startup-project D:\a\1\s\src\MyProject.WebApi\MyProject.WebApi.csproj
##[debug]AGENT_VERSION: '2.193.1'
##[debug]AGENT_TEMPDIRECTORY: 'D:\a\_temp'
##[debug]Asserting container path exists: 'D:\a\_temp'
##[debug]Asserting leaf path exists: 'C:\Windows\system32\cmd.exe'
========================== Starting Command Output ===========================
##[debug]Entering Invoke-VstsTool.
##[debug] Arguments: '/D /E:ON /V:OFF /S /C "CALL "D:\a\_temp\37fc4a71-a144-4332-9a84-04e6138a2538.cmd""'
##[debug] FileName: 'C:\Windows\system32\cmd.exe'
##[debug] WorkingDirectory: 'D:\a\1\s'
"C:\Windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "D:\a\_temp\37fc4a71-a144-4332-9a84-04e6138a2538.cmd""

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the applicgation service provider. Error: A certificate with the thumbprint 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' could not be found.
Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

##[debug]Exit code: 1
##[debug]Leaving Invoke-VstsTool.
##[error]Cmd.exe exited with code '1'.
##[debug]Processed: ##vso[task.logissue type=error]Cmd.exe exited with code '1'.
##[debug]Processed: ##vso[task.complete result=Failed]Error detected
##[debug]Leaving D:\a\_tasks\CmdLine_d9bafed4-0b18-4f58-968d-86655b4d2ce9\2.182.0\cmdline.ps1.
Finishing: CmdLine

有时,通过调整 YAML 文件,我能够摆脱第一个错误,但第二个错误在 devops 上从未消失。问题很大程度上是因为有单独的迁移项目,但我认为应该是这样......

我的构建管道的 YAML:

trigger:
- develop

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: DotNetCoreCLI@2
  displayName: Restore
  inputs:
    command: restore
    projects: '**/MyProject.WebApi.csproj'

- task: DotNetCoreCLI@2
  displayName: Build
  inputs:
    projects: '**/MyProject.WebApi.csproj'
    arguments: '--no-restore'

- task: DotNetCoreCLI@2
  displayName: Test
  inputs:
    command: test
    projects: '**/*[Tt]ests/*.csproj'
    arguments: '--no-restore --no-build'

- task: DotNetCoreCLI@2
  displayName: 'Publish WebApi'
  inputs:
    command: publish
    publishWebProjects: false
    projects: '**/MyProject.WebApi.csproj'
    arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory) --runtime -r $(runtime)'

- task: CopyFiles@2
  inputs:
    Contents: '**'
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

- task: UseDotNet@2
  inputs:
    packageType: 'sdk'
    version: '5.x'

- task: DotNetCoreCLI@2
  displayName: Install dotnet-ef
  inputs:
    command: 'custom'
    custom: 'tool'
    arguments: 'install --global dotnet-ef --version 5.0.10 --ignore-failed-sources'

- task: CmdLine@2
  inputs:
    script: dotnet ef migrations script --no-build -o $(Build.ArtifactStagingDirectory)\migrations\script.sql --idempotent --project $(Build.SourcesDirectory)\src\MyProject.MigrationProject\MyProject.MigrationProject.csproj --startup-project $(Build.SourcesDirectory)\src\MyProject.WebApi\MyProject.WebApi.csproj

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: drop'

我的怀疑:

执行命令的目录可能存在问题(在 ADO powershell 上)。我怀疑这是因为,在我的本地机器上,在调用方法 x.MigrationsAssembly("MyProject.MigrationProject") 之前,当我从入口项目目录以外的目录执行以下命令时,以下命令失败,但是当我将 powershell 导航到 entry 项目并执行相同的命令时,它成功了。当时的命令是:

dotnet ef migrations script -o D:\migrations\script.sql --idempotent --project D:\...\src\MyProject.MigrationProject\MyProject.MigrationProject.csproj

我已经在另一个项目中使用了相同的 YAML,但它包含单个 Web API 项目中的所有内容,因此我没有遇到任何问题。

问题:

我在这里做错了什么?我能做些什么来解决这个问题?任何帮助将不胜感激。

项目详情

点网 5.0

EntityFramewokCore 5.0.10

Visual Studio 2019

如果我遗漏了什么,请询问。

更新:

我对执行 dotnet ef 命令的工作目录的怀疑似乎是错误的,因为我通过向命令行任务提供 workingDirectory 参数来尝试这样做。不过它可以在本地机器上运行。

【问题讨论】:

    标签: .net-core azure-devops continuous-integration migration .net-5


    【解决方案1】:

    谢谢@jane-ma-msft

    错误消息显示这是一个证书错误。请按照变通方法解决问题。

    1. 尝试生成新证书或取消证书验证。

    2. 检查您的.sln 文件。如果它有 PackageCertificateKeyFilePackageCertificateThumbprint 请尝试删除属性并重新启动管道。

      或检查它是否配置正确,并且您已将正确的证书文件上传到适当的路径。

    3. 确保代理 windows-latest 拥有您需要的所有 .NET SDK 版本以及您的项目需要引用的所有软件。如果没有,请使用任务或命令行下载它们。点击this link查看windows-latest代理上安装的软件。

    4. 如果您在管道中使用 Microsoft 托管的 Windows 代理,请尝试在您的管道中使用自托管代理。详细步骤请点击this document

    参考这里Link 1 & Link 2

    【讨论】:

    • 谢谢@DelliganeshS-MT!目前,在我的测试机器上,我不需要调用修复证书错误的 ProtectKeysWithCertificate 方法。稍后,我将进行建议的更改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多