Search code examples
entity-framework-coreazure-pipelines

Azure DevOps Pipelines: dotnet ef not working


I am trying to set up EF Core migrations within my build-pipeline. Reading through online documentation and examples leads me to believe that this should be a straightforward thing to accomplish, however I am getting stuck whilst still installing the tools.

I start by installing the tools as required:

  - task: DotNetCoreCLI@2
    displayName: Install EF Tool
    inputs:
      command: custom
      custom: 'tool'
      arguments: 'update --global dotnet-ef' 

Now this step (above) always succeeds, but frequently (not always though) it also comes with some additional information about the PATH variables:

"C:\Program Files\dotnet\dotnet.exe" tool update --global dotnet-ef
Tools directory 'C:\Windows\ServiceProfiles\NetworkService\.dotnet\tools' is not currently on the PATH environment variable.

You can add the directory to the PATH by running the following command:

setx PATH "%PATH%;C:\Windows\ServiceProfiles\NetworkService\.dotnet\tools"

You can invoke the tool using the following command: dotnet-ef
Tool 'dotnet-ef' (version '8.0.7') was successfully installed.

Which caused me to add another step to update the PATH as instructed. This step also always success:

  - script: setx PATH "%PATH%;C:\Windows\ServiceProfiles\NetworkService\.dotnet\tools"
    displayName: Add toolsdirectory to Path 

Now we get to the part where things go wrong. I verify that EF Core has been successfully installed by using dotnet ef, which should give the EF Core unicorn horsey output if everything is working as expected.

  - task: DotNetCoreCLI@2
    displayName: Verify EF installation
    inputs:
      command: custom
      custom: 'ef'  

but things come unstuck for reasons I cannot explain.

Could not execute because the specified command or file was not found.
    Possible reasons for this include:
      * You misspelled a built-in dotnet command.
      * You intended to execute a .NET program, but dotnet-ef does not exist.
      * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.

##\[error\]Error: The process 'C:\Program Files\dotnet\dotnet.exe' failed with exit code 1

installing EF Tools seems to work as expected using dotnet ef still goes wrong


Solution

  • Based on your description, it seemed that your pipeline was running a Windows self-hosted agent as the local service account of NetworkService. I could reproduce similar issue when running my pipeline on such an agent.

    Image

    For this you may add a UseDotNet@2 task in front of the step to install dotnet ef, which could add C:\Windows\ServiceProfiles\NetworkService\.dotnet\tools to global tool path.

    pool: Default
    #  vmImage: 'windows-latest'
    
    variables:
      system.debug: true
    
    steps:
    - task: UseDotNet@2
      inputs:
        packageType: 'sdk'
        version: '8.x'
    - task: DotNetCoreCLI@2
      displayName: Install EF Tool
      inputs:
        command: custom
        custom: 'tool'
        arguments: 'update --global dotnet-ef' 
    - task: DotNetCoreCLI@2
      displayName: Verify EF installation
      inputs:
        command: custom
        custom: 'ef'
    
    
    

    Image