首页 > 解决方案 > EF Core 3.1 迁移工具因依赖 .NET Core 2.0 而失败

问题描述

我有一个使用 EF Core 3.1 的项目。启动项目是使用 V3 和框架 3.1 运行的 Azure 函数。其余项目是 .NET Standard 2.0 项目的类库。

我正在尝试为 EF Core 3.1 生成迁移脚本,但我从 Azure DevOps 收到以下错误

Starting: Run EF Core Migrations
==============================================================================
Task         : Command line
Description  : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
Version      : 2.163.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
==============================================================================
Generating script.
Script contents:
dotnet ef migrations script --startup-project C:\vstsagent\A6\_work\2\s/MyProject/MyProject.Function.WebApi/MyProject.Function.WebApi.csproj --output C:\vstsagent\A6\_work\2\a/16138/Migrations/SqlScript.sql --context $dbContext --idempotent --verbose --framework netcoreapp3.1
========================== Starting Command Output ===========================
"C:\Windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "C:\vstsagent\A6\_work\_temp\3930c14d-f779-4939-9748-dd2ab8c6baa0.cmd""
Using project 'C:\vstsagent\A6\_work\2\s/MyProject/MyProject.Function.WebApi/MyProject.Function.WebApi.csproj'.
Using startup project 'C:\vstsagent\A6\_work\2\s/MyProject/MyProject.Function.WebApi/MyProject.Function.WebApi.csproj'.
Writing 'C:\vstsagent\A6\_work\2\s\MyProject\MyProject.Function.WebApi\obj\MyProject.Function.WebApi.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Windows\ServiceProfiles\NetworkService\AppData\Local\Temp\tmp74C6.tmp /verbosity:quiet /nologo C:\vstsagent\A6\_work\2\s/MyProject/MyProject.Function.WebApi/MyProject.Function.WebApi.csproj
Writing 'C:\vstsagent\A6\_work\2\s\MyProject\MyProject.Function.WebApi\obj\MyProject.Function.WebApi.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Windows\ServiceProfiles\NetworkService\AppData\Local\Temp\tmp77C5.tmp;TargetFramework=netcoreapp3.1 /verbosity:quiet /nologo C:\vstsagent\A6\_work\2\s/MyProject/MyProject.Function.WebApi/MyProject.Function.WebApi.csproj
Build started...

Build succeeded.
dotnet exec --depsfile C:\vstsagent\A6\_work\2\s\MyProject\MyProject.Function.WebApi\bin\Debug\netcoreapp3.1\MyProject.Function.WebApi.deps.json --additionalprobingpath C:\Windows\ServiceProfiles\NetworkService\.nuget\packages C:\Windows\ServiceProfiles\NetworkService\.dotnet\tools\.store\dotnet-ef\3.1.1\dotnet-ef\3.1.1\tools\netcoreapp3.1\any\tools\netcoreapp2.0\any\ef.dll migrations script --output C:\vstsagent\A6\_work\2\a/16138/Migrations/covImsSqlScript.sql --context $dbContext --idempotent --assembly C:\vstsagent\A6\_work\2\s\MyProject\MyProject.Function.WebApi\bin\Debug\netcoreapp3.1\MyProject.Function.WebApi.dll --startup-assembly C:\vstsagent\A6\_work\2\s\MyProject\MyProject.Function.WebApi\bin\Debug\netcoreapp3.1\MyProject.Function.WebApi.dll --project-dir C:\vstsagent\A6\_work\2\s\MyProject\MyProject.Function.WebApi\ --language C# --working-dir C:\vstsagent\A6\_work\2\s --verbose --root-namespace MyProject.Function.WebApi
It was not possible to find any compatible framework version
The framework 'Microsoft.NETCore.App', version '2.0.0' was not found.
  - The following frameworks were found:
      3.1.0 at [C:\vstsagent\A6\_work\_tool\dotnet\shared\Microsoft.NETCore.App]
      3.1.1 at [C:\vstsagent\A6\_work\_tool\dotnet\shared\Microsoft.NETCore.App]

You can resolve the problem by installing the specified framework and/or SDK.

The specified framework can be found at:
  - https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=2.0.0&arch=x64&rid=win81-x64
##[error]Cmd.exe exited with code '-2147450730'.
Finishing: Run EF Core Migrations

当我查看执行dotnet.exe tool install dotnet-ef -g时安装的工具的文件夹时,我可以看到有一个目录仍在引用框架 2.0,该目录存在于 dotnet ef 的命令中迁移脚本运行。

C:\Windows\ServiceProfiles\NetworkService\.dotnet\tools\.store\dotnet-ef\3.1.1\dotnet-ef\3.1.1\tools\netcoreapp3.1\any\tools\netcoreapp2.0\any\ef.dll

我唯一能想到的是 EF Core 3.1 需要 .NET Core 2.0 也安装在构建服务器上才能正常工作。但是,这感觉不对。

有没有人经历过类似的事情,除了安装 .NET Core 2.0 之外,您是否设法找到了解决方案?

标签: c#entity-framework.net-coreentity-framework-migrationsef-core-3.1

解决方案


我想我找到了问题所在。我错过了 --project 参数。然而,一旦我把它我收到以下错误:

Error:
  An assembly specified in the application dependencies manifest (MyProject.Function.WebApi.deps.json) was not found:
    package: 'MyProject.Common', version: '1.0.0'
    path: 'MyProject.Common.dll'

看起来 EF Core 迁移工具不喜欢作为 Azure 函数项目的启动项目。由于迁移项目是 .NET Standard,我所做的是创建一个什么都不做的控制台项目(只是一个带有 Console.Writelne("") 的 Program.CS),并使其引用包含迁移的项目并将其用作我的启动项目。通过这样做,迁移工作正常。


推荐阅读