首页 > 解决方案 > 如何修复:在 Azure Devops 构建管道实体框架不生成 SQL 脚本

问题描述

我正在尝试为项目准备 azure 构建管道,但在 Azure Devops 中生成的 SQL 脚本不包含任何必需的数据方案。

更多细节。项目在 Aspnet 核心 2.2 中并使用实体框架。当我在本地运行命令时,我会收到正确的 SQL 脚本:

dotnet ef migrations script -i -c TrainersContext -p .\WebApplication\PersonalTrainer.WebApp.Core\PersonalTrainer.WebApp.Core.csproj -o .\migrations\TrainersContextScripts.sql -s .\WebApplication\PersonalTrainer.WebApp.Core\PersonalTrainer.WebApp.Core.csproj

dotnet ef migrations script -i -c PersonalTrainerWebContext -p .\WebApplication\PersonalTrainer.WebApp.Core\PersonalTrainer.WebApp.Core.csproj -o .\migrations\PersonalTrainerWebContextScripts.sql -s .\WebApplication\PersonalTrainer.WebApp.Core\PersonalTrainer.WebApp.Core.csproj

当我将命令作为 Azure Devops 管道的一部分运行时:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      dotnet ef migrations script -i -v -c PersonalTrainerWebContext -p $(Build.SourcesDirectory)\WebApplication\PersonalTrainer.WebApp.Core\PersonalTrainer.WebApp.Core.csproj -o $(build.artifactstagingdirectory)\migrations\PersonalTrainerWebContextScripts.sql -s $(Build.SourcesDirectory)\WebApplication\PersonalTrainer.WebApp.Core\PersonalTrainer.WebApp.Core.csproj
      dotnet ef migrations script -i -v -c TrainersContext -p $(Build.SourcesDirectory)\WebApplication\PersonalTrainer.WebApp.Core\PersonalTrainer.WebApp.Core.csproj -o $(build.artifactstagingdirectory)\migrations\TrainersContextScripts.sql -s $(Build.SourcesDirectory)\WebApplication\PersonalTrainer.WebApp.Core\PersonalTrainer.WebApp.Core.csproj

输出只有:

IF OBJECT_ID(N'[__EFMigrationsHistory]') IS NULL
BEGIN
    CREATE TABLE [__EFMigrationsHistory] (
        [MigrationId] nvarchar(150) NOT NULL,
        [ProductVersion] nvarchar(32) NOT NULL,
        CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
    );
END;

GO

详细的输出:

Successfully installed the ASP.NET Core HTTPS Development Certificate.
To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only). For establishing trust on other platforms refer to the platform specific documentation.
For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.
Using project 'd:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\PersonalTrainer.WebApp.Core.csproj'.
Using startup project 'd:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\PersonalTrainer.WebApp.Core.csproj'.
Writing 'd:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\obj\PersonalTrainer.WebApp.Core.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\VssAdministrator\AppData\Local\Temp\tmpCD60.tmp /verbosity:quiet /nologo d:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\PersonalTrainer.WebApp.Core.csproj
Writing 'd:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\obj\PersonalTrainer.WebApp.Core.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\VssAdministrator\AppData\Local\Temp\tmpD32D.tmp /verbosity:quiet /nologo d:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\PersonalTrainer.WebApp.Core.csproj
dotnet build d:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\PersonalTrainer.WebApp.Core.csproj /verbosity:quiet /nologo

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:11.45
dotnet exec --depsfile d:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\bin\Debug\netcoreapp2.2\PersonalTrainer.WebApp.Core.deps.json --additionalprobingpath C:\Users\VssAdministrator\.nuget\packages --additionalprobingpath C:\Microsoft\Xamarin\NuGet --additionalprobingpath C:\hostedtoolcache\windows\dotnet\sdk\NuGetFallbackFolder --runtimeconfig d:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\bin\Debug\netcoreapp2.2\PersonalTrainer.WebApp.Core.runtimeconfig.json C:\hostedtoolcache\windows\dotnet\sdk\2.2.203\DotnetTools\dotnet-ef\2.2.4\tools\netcoreapp2.2\any\tools\netcoreapp2.0\any\ef.dll migrations script -i -c PersonalTrainerWebContext -o d:\a\1\a\migrations\PersonalTrainerWebContextScripts.sql --assembly d:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\bin\Debug\netcoreapp2.2\PersonalTrainer.WebApp.Core.dll --startup-assembly d:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\bin\Debug\netcoreapp2.2\PersonalTrainer.WebApp.Core.dll --project-dir d:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\ --language C# --working-dir D:\a\1\s --verbose --root-namespace PersonalTrainer.WebApp.Core
Using assembly 'PersonalTrainer.WebApp.Core'.
Using startup assembly 'PersonalTrainer.WebApp.Core'.
Using application base 'd:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\bin\Debug\netcoreapp2.2'.
Using working directory 'd:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core'.
Using root namespace 'PersonalTrainer.WebApp.Core'.
Using project directory 'd:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\'.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider...
Finding IWebHost accessor...
Using environment 'Development'.
Using application service provider from IWebHost accessor on 'Program'.
Found DbContext 'PersonalTrainerWebContext'.
Found DbContext 'TrainersContext'.
Finding DbContext classes in the project...
Using context 'PersonalTrainerWebContext'.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding design-time services referenced by assembly 'PersonalTrainer.WebApp.Core'.
No referenced design-time services were found.
Finding IDesignTimeServices implementations in assembly 'PersonalTrainer.WebApp.Core'...
No design-time services were found.
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 2.2.4-servicing-10062 initialized 'PersonalTrainerWebContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: MigrationsAssembly=PersonalTrainer.WebApp.Core 
Writing 'd:\a\1\a\migrations\PersonalTrainerWebContextScripts.sql'...
Using project 'd:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\PersonalTrainer.WebApp.Core.csproj'.
Using startup project 'd:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\PersonalTrainer.WebApp.Core.csproj'.
Writing 'd:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\obj\PersonalTrainer.WebApp.Core.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\VssAdministrator\AppData\Local\Temp\tmpC9B.tmp /verbosity:quiet /nologo d:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\PersonalTrainer.WebApp.Core.csproj
Writing 'd:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\obj\PersonalTrainer.WebApp.Core.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\VssAdministrator\AppData\Local\Temp\tmp10F2.tmp /verbosity:quiet /nologo d:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\PersonalTrainer.WebApp.Core.csproj
dotnet build d:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\PersonalTrainer.WebApp.Core.csproj /verbosity:quiet /nologo

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:04.72
dotnet exec --depsfile d:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\bin\Debug\netcoreapp2.2\PersonalTrainer.WebApp.Core.deps.json --additionalprobingpath C:\Users\VssAdministrator\.nuget\packages --additionalprobingpath C:\Microsoft\Xamarin\NuGet --additionalprobingpath C:\hostedtoolcache\windows\dotnet\sdk\NuGetFallbackFolder --runtimeconfig d:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\bin\Debug\netcoreapp2.2\PersonalTrainer.WebApp.Core.runtimeconfig.json C:\hostedtoolcache\windows\dotnet\sdk\2.2.203\DotnetTools\dotnet-ef\2.2.4\tools\netcoreapp2.2\any\tools\netcoreapp2.0\any\ef.dll migrations script -i -c TrainersContext -o d:\a\1\a\migrations\TrainersContextScripts.sql --assembly d:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\bin\Debug\netcoreapp2.2\PersonalTrainer.WebApp.Core.dll --startup-assembly d:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\bin\Debug\netcoreapp2.2\PersonalTrainer.WebApp.Core.dll --project-dir d:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\ --language C# --working-dir D:\a\1\s --verbose --root-namespace PersonalTrainer.WebApp.Core
Using assembly 'PersonalTrainer.WebApp.Core'.
Using startup assembly 'PersonalTrainer.WebApp.Core'.
Using application base 'd:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\bin\Debug\netcoreapp2.2'.
Using working directory 'd:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core'.
Using root namespace 'PersonalTrainer.WebApp.Core'.
Using project directory 'd:\a\1\s\WebApplication\PersonalTrainer.WebApp.Core\'.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider...
Finding IWebHost accessor...
Using environment 'Development'.
Using application service provider from IWebHost accessor on 'Program'.
Found DbContext 'PersonalTrainerWebContext'.
Found DbContext 'TrainersContext'.
Finding DbContext classes in the project...
Using context 'TrainersContext'.
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 2.2.4-servicing-10062 initialized 'TrainersContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: MigrationsAssembly=PersonalTrainer.WebApp.Core 
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding design-time services referenced by assembly 'PersonalTrainer.WebApp.Core'.
No referenced design-time services were found.
Finding IDesignTimeServices implementations in assembly 'PersonalTrainer.WebApp.Core'...
No design-time services were found.
Writing 'd:\a\1\a\migrations\TrainersContextScripts.sql'...
##[section]Finishing: PowerShell

我在这里做错了什么吗?

标签: asp.net-coreazure-devopsentity-framework-core

解决方案


您需要“迁移”文件夹来生成脚本。

如果存储库中没有该Migrations文件夹,请在生成脚本之前进行迁移:

dotnet ef migrations add X

推荐阅读