You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Pipeline执行EF迁移脚本时缺失csproj.EntityFrameworkCore.targets文件

问题:Azure Pipeline生成EF迁移SQL脚本失败,缺失.csproj.EntityFrameworkCore.targets文件

在Azure Pipeline中使用DotNetCoreCLI@2执行ef migrations script命令时,报错提示D:\a\1\s\HR Taxonomy Change Management\obj\目录下缺失csproj.EntityFrameworkCore.targets文件。本地仓库对应目录存在该文件,但构建代理上未生成。已尝试MSBuild@1和VSBuild@1构建解决方案,问题依旧。


错误的EF脚本生成任务配置

- task: DotNetCoreCLI@2
  displayName: 'Generate EF SQL Script'
  inputs:
    command: 'custom'
    custom: 'ef'
    arguments: 'migrations script --project "$(Build.SourcesDirectory)\HR Taxonomy Change Management"\*.csproj --output $(build.artifactStagingDirectory)\script.sql  --idempotent'

错误日志

Task         : .NET Core
Description  : Build, test, package, or publish a dotnet application, or run a custom dotnet command
Version      : 2.232.0
Author       : Microsoft Corporation
==============================================================================
C:\Windows\system32\chcp.com 65001
Active code page: 65001
Info: .NET Core SDK/runtime 2.2 and 3.0 are now End of Life(EOL) and have been removed from all hosted agents. If you're using these SDK/runtimes on hosted agents, kindly upgrade to newer versions which are not EOL, or else use UseDotNet task to install the required version.
"C:\Program Files\dotnet\dotnet.exe" ef migrations script --project "D:\a\1\s\HR Taxonomy Change Management\*.csproj" --output D:\a\1\a\script.sql --idempotent
System.IO.IOException: The filename, directory name, or volume label syntax is incorrect. : 'D:\a\1\s\HR Taxonomy Change Management\obj\*.csproj.EntityFrameworkCore.targets'
   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategy(FileStream fileStream, String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize)
   at System.IO.File.OpenWrite(String path)
   at Microsoft.EntityFrameworkCore.Tools.Project.FromFile(String file, String buildExtensionsDir, String framework, String configuration, String runtime)
   at Microsoft.EntityFrameworkCore.Tools.RootCommand.Execute(String[] _)
   at Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase.<>c__DisplayClass0_0.<Configure>b__0(String[] args)
   at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
   at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)
The filename, directory name, or volume label syntax is incorrect. : 'D:\a\1\s\HR Taxonomy Change Management\obj\*.csproj.EntityFrameworkCore.targets'
##[error]Error: The process 'C:\Program Files\dotnet\dotnet.exe' failed with exit code 1
Info: Azure Pipelines hosted agents have been updated and now contain .Net 5.x SDK/Runtime along with the older .Net Core version which are currently lts. Unless you have locked down a SDK version for your project(s), 5.x SDK might be picked up which might have breaking behavior as compared to previous versions.
##[error]Dotnet command failed with non-zero exit code on the following projects : [ '' ]
Finishing: Generate EF SQL Script

完整原YAML流水线配置

# ASP.NET Core (.NET Framework)
# Build and test ASP.NET Core projects targeting the full .NET Framework.

trigger:
- master

pool:
  vmImage: 'windows-latest'

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

steps:
- task: NuGetToolInstaller@1
    
- task: NuGetCommand@2
  displayName: Nuget Restore
  inputs:
    command: 'restore'
    restoreSolution: '$(solution)'
    feedsToUse: 'select'
    vstsFeed: '33581106-1fd6-4092-9693-a20196c22142/53f90c9a-62a2-49cc-91c6-9ae04c4ca4fd'

- task: CmdLine@2
  displayName: 'dir D:/a/1/s/.git/'
  inputs:
    script: 
      'dir $(Build.SourcesDirectory)\.git\'


# - task: VSBuild@1
#   displayName: 'Build Solution'
#   inputs:
#     solution: '$(solution)'
#     # msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"'
#     platform: '$(buildPlatform)'
#     configuration: '$(buildConfiguration)'

- task: MSBuild@1
  inputs:
    solution: '**/*.sln'
    platform: '$(buildPlatform)'

- task: DotNetCoreCLI@2
  displayName: 'Install EF tool'
  inputs:
    command: 'custom'
    custom: 'tool'
    arguments: 'install dotnet-ef --global --version 7.0.5'
    workingDirectory: '$(Agent.BuildDirectory)'

- task: DotNetCoreCLI@2
  displayName: 'Generate EF SQL Script'
  inputs:
    command: 'custom'
    custom: 'ef'
    arguments: 'migrations script --project "$(Build.SourcesDirectory)\HR Taxonomy Change Management"\*.csproj --output $(build.artifactStagingDirectory)\script.sql  --idempotent'

- task: VSTest@2
  displayName: 'Running Unit Tests'
  inputs:
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: SqlAzureDacpacDeployment@1
  displayName: 'Update Database'
  inputs:
    azureSubscription: 'HR Data Governance (2442165c-339d-4e83-a443-a6583c372938)'
    AuthenticationType: 'servicePrincipal'
    ServerName: 'hrchangemanagement'
    DatabaseName: 'hrchangemanagement'
    deployType: 'SqlTask'
    SqlFile: '$(build.artifactStagingDirectory)\obj\script.sql'

# - task: AzureRmWebAppDeployment@4
#   inputs:
#     ConnectionType: 'AzureRM'
#     azureSubscription: 'HR Data Governance (2442165c-339d-4e83-a443-a6583c372938)'
#     appType: 'webApp'
#     WebAppName: 'HRTaxonomyChangeManagement'
#     packageForLinux: '$(build.artifactStagingDirectory)/**/*.zip'

# - task: TSAUpload@2
#   inputs:
#     GdnPublishTsaOnboard: true
#     GdnPublishTsaConfigFile: '**/*'

修复步骤

1. 修正项目路径中的通配符问题

错误核心原因是路径中使用*.csproj导致EF工具无法解析正确的目标文件路径。需替换为具体项目文件名:

arguments: 'migrations script --project "$(Build.SourcesDirectory)\HR Taxonomy Change Management\YourProjectName.csproj" --output $(build.artifactStagingDirectory)\script.sql --idempotent'

若不想硬编码项目名,可通过变量统一管理:

variables:
  efProjectPath: '$(Build.SourcesDirectory)\HR Taxonomy Change Management\*.csproj'

# EF任务中使用变量
arguments: 'migrations script --project "$(efProjectPath)" --output $(build.artifactStagingDirectory)\script.sql --idempotent'

2. 确保构建任务生成EF目标文件

修改MSBuild@1任务,添加完整构建参数,确保生成EF所需的目标文件:

- task: MSBuild@1
  inputs:
    solution: '**/*.sln'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    msbuildArguments: '/t:Build /restore'

或改用DotNetCoreCLI@2的build命令,让.NET SDK自动处理EF依赖:

- task: DotNetCoreCLI@2
  displayName: 'Build Solution'
  inputs:
    command: 'build'
    projects: '$(solution)'
    arguments: '--configuration $(buildConfiguration)'

3. 调整EF工具安装方式

全局安装易引发版本冲突,建议改为本地安装:

- task: DotNetCoreCLI@2
  displayName: 'Install EF tool locally'
  inputs:
    command: 'custom'
    custom: 'tool'
    arguments: 'install dotnet-ef --version 7.0.5'
    workingDirectory: '$(Build.SourcesDirectory)'

4. 验证目标文件生成

在EF命令前添加检查任务,确认obj目录是否生成所需文件:

- task: CmdLine@2
  displayName: 'Check EF targets file'
  inputs:
    script: 'dir "$(Build.SourcesDirectory)\HR Taxonomy Change Management\obj\"'
    workingDirectory: '$(Build.SourcesDirectory)'

内容的提问来源于stack exchange,提问作者USMC6072

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 01:15:10