Azure DevOps构建Debug/Release DLL及EF Core迁移脚本问题咨询
Azure DevOps EF Core迁移脚本构建问题解答
问题背景
在Azure DevOps经典构建流水线中,我们复用任务组生成EntityFramework Core迁移脚本(迁移由发布流水线执行),任务组内的PowerShell任务代码如下:
if ("$(filePathToProjFolder)" -ne "-" -and "$(dbcontexts)" -ne "-") { dotnet tool install --global dotnet-ef $dbcontexts = "$(dbcontexts)" -split "\|" foreach ($dbcontext in $dbcontexts) { dotnet ef migrations script -i -o $(build.artifactstagingdirectory)\migrations-$dbcontext.sql --project $(filePathToProjFolder) --context $dbcontext --no-build } }
其中一条流水线运行正常,但另一条触发报错:
The specified deps.json [C:\agents\dev\_work\237\s\<projectName>\bin\Debug\net8.0\<projectName>.deps.json] does not exist
经日志排查:正常流水线会同时生成Release和Debug版本的DLL,而失败的流水线仅生成Release版本的DLL。
1. 能否强制迁移脚本使用Release版本的DLL?
可以。dotnet ef命令默认采用Debug配置生成迁移脚本,只需在命令中添加--configuration参数指定目标配置即可。推荐使用流水线变量$(BuildConfiguration),适配流水线的配置设置,无需硬编码,修改后的PowerShell代码如下:
if ("$(filePathToProjFolder)" -ne "-" -and "$(dbcontexts)" -ne "-") { dotnet tool install --global dotnet-ef $dbcontexts = "$(dbcontexts)" -split "\|" foreach ($dbcontext in $dbcontexts) { dotnet ef migrations script -i -o $(build.artifactstagingdirectory)\migrations-$dbcontext.sql --project $(filePathToProjFolder) --context $dbcontext --no-build --configuration $(BuildConfiguration) } }
2. 为何正常流水线会同时生成两种版本的DLL?
正常流水线同时生成Debug和Release版本DLL的原因通常有以下几种:
- 流水线中存在多个构建任务:除了当前指定
$(BuildConfiguration)的dotnet build任务,还有另一个针对Debug配置的构建任务; - 项目文件配置:目标项目的
.csproj文件中定义了多输出配置,例如在<PropertyGroup>节点添加了<Configurations>Debug;Release</Configurations>,触发多版本构建; - 启用多配置构建:流水线开启了多配置构建(Multi-configuration)模式,同时针对Debug和Release配置执行构建作业。
3. 如何强制失败的流水线同时生成两种版本的DLL?
针对Azure DevOps经典流水线,可通过以下两种方式实现:
方式一:添加多轮构建任务
在流水线的构建阶段添加两个DotNetCoreCLI@2构建任务:
- 第一个任务的
arguments配置为--configuration Debug --no-restore; - 第二个任务的
arguments配置为--configuration Release --no-restore;
注:若流水线中未单独配置Restore任务,需移除
--no-restore参数。
方式二:启用多配置构建模式
- 打开经典流水线的选项页面;
- 找到多配置设置,勾选「启用多配置」;
- 在「变量」字段输入
BuildConfiguration=Debug,Release; - 保存流水线后,系统会自动针对每个配置执行构建任务,生成对应版本的DLL。
内容的提问来源于stack exchange,提问作者David
相关产品推荐
相关产品推荐

