【问题标题】:Entity Framkework migaration add failing实体框架迁移添加失败
【发布时间】:2018-11-17 00:46:30
【问题描述】:

我正在使用 .Net Core 2 和 Angular 5 构建一个测试应用程序(学习)。

从 csproj 文件所在的根文件夹中,在 powerShell 中执行以下操作会导致错误

命令

dotnet ef migrations add "Initial" -o "Data\Migrations"

错误

PS C:\Users\T460SU\Documents\Training\Learning-Examples\Api-Angular2018\NetCore2Angular5\TestMakerFreeApp\TestMakerFreeApp> dotnet ef migrations add "Initial" -o "Data\Migrations"
No executable found matching command "dotnet-ef"

在浏览了各种帖子后,我好像错过了Microsoft.EntityFrameworkCore.Tools.DotNet。当我尝试安装此软件包时,会导致以下错误

PM> Install-Package Microsoft.EntityFrameworkCore.Tools.DotNet -Version 2.0.3
  GET https://api.nuget.org/v3/registration3-gz-semver2/microsoft.entityframeworkcore.tools.dotnet/index.json
  OK https://api.nuget.org/v3/registration3-gz-semver2/microsoft.entityframeworkcore.tools.dotnet/index.json 70ms
Restoring packages for C:\Users\T460SU\Documents\Training\Learning-Examples\Api-Angular2018\NetCore2Angular5\TestMakerFreeApp\TestMakerFreeApp\TestMakerFreeApp.csproj...
Install-Package : Detected package downgrade: Microsoft.NETCore.App from 2.0.7 to 2.0.0. Reference the package directly from the project to select a different version. 
 TestMakerFreeApp -> Microsoft.EntityFrameworkCore.Tools.DotNet 2.0.3 -> Microsoft.NETCore.App (>= 2.0.7) 
 TestMakerFreeApp -> Microsoft.NETCore.App (>= 2.0.0)
At line:1 char:1
+ Install-Package Microsoft.EntityFrameworkCore.Tools.DotNet -Version 2 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Install-Package], Exception
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand

Install-Package : Package 'Microsoft.EntityFrameworkCore.Tools.DotNet 2.0.3' has a package type 'DotnetCliTool' that is not supported by project 'TestMakerFreeApp'.
At line:1 char:1
+ Install-Package Microsoft.EntityFrameworkCore.Tools.DotNet -Version 2 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Install-Package], Exception
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand

Time Elapsed: 00:00:01.1203778

这里是当前安装的 nuget 包

我该如何解决这个问题? 更新 1:csproj 文件

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
    <TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.2" />
  </ItemGroup>

  <ItemGroup>
    <!-- Files not to publish (note that the 'dist' subfolders are re-added below) -->
    <Content Remove="ClientApp\**" />
  </ItemGroup>

  <ItemGroup>
    <None Remove="ClientApp\app\components\about\about.component.ts" />
    <None Remove="ClientApp\app\components\login\login.component.ts" />
    <None Remove="ClientApp\app\components\pagenotfound\pagenotfound.component.ts" />
    <None Remove="ClientApp\app\components\quiz\quiz-list.component.ts" />
    <None Remove="ClientApp\app\components\quiz\quiz.component.ts" />
    <None Remove="ClientApp\app\interfaces\quiz.ts" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.5" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.0" />
  </ItemGroup>

  <ItemGroup>
    <TypeScriptCompile Include="ClientApp\app\components\about\about.component.ts" />
    <TypeScriptCompile Include="ClientApp\app\components\login\login.component.ts" />
    <TypeScriptCompile Include="ClientApp\app\components\pagenotfound\pagenotfound.component.ts" />
    <TypeScriptCompile Include="ClientApp\app\components\quiz\quiz-list.component.ts" />
    <TypeScriptCompile Include="ClientApp\app\components\quiz\quiz.component.ts" />
    <TypeScriptCompile Include="ClientApp\app\interfaces\quiz.ts" />
  </ItemGroup>

  <Target Name="DebugRunWebpack" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('wwwroot\dist') ">
    <!-- Ensure Node.js is installed -->
    <Exec Command="node --version" ContinueOnError="true">
      <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
    </Exec>
    <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />

    <!-- In development, the dist files won't exist on the first run or when cloning to
         a different machine, so rebuild them if not already present. -->
    <Message Importance="high" Text="Performing first-run Webpack build..." />
    <Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js" />
    <Exec Command="node node_modules/webpack/bin/webpack.js" />
  </Target>

  <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
    <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec Command="npm install" />
    <Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod" />
    <Exec Command="node node_modules/webpack/bin/webpack.js --env.prod" />

    <!-- Include the newly-built files in the publish output -->
    <ItemGroup>
      <DistFiles Include="wwwroot\dist\**; ClientApp\dist\**" />
      <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
        <RelativePath>%(DistFiles.Identity)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
      </ResolvedFileToPublish>
    </ItemGroup>
  </Target>

</Project>

【问题讨论】:

  • 在进一步阅读之后,看起来我不需要这个包。所以现在我完全不知道这里可能出了什么问题!
  • 看起来它无法识别可能发生的 dotnet 关键字,因为您不在项目文件夹中,即 Path: ~/Solution/Project/Controllers 项目文件夹是您需要定位的位置,所以如果第二个TestMakerFreeApp 是项目中的 Angular 应用程序,这是您的问题。如果它们不工作,你是否能够自己运行 dotnet(尝试:dotnet -h),它必须对环境变量做一些事情。如果您在项目文件夹中并且 dotnet 命令正在运行,则似乎缺少工具。你能仔细检查一下吗?
  • 我现在还没有设置单独的项目,它是一个单独的项目。第一个 TestMakerFreeApp 是父文件夹,第二个包含所有文件。像控制器和视图等...今晚晚些时候下班后我会仔细检查。
  • 我可以在同一位置运行 dotnet -h 而不会出现任何问题

标签: entity-framework visual-studio-2017 .net-core


【解决方案1】:

能否请您整合您的 .net 核心版本,这样您就可以拥有一个全新的版本。

如果您使用的是 2.0.x(或更低版本),则需要将其添加到包含数据库逻辑的项目的项目文件 (csproj) 中

<ItemGroup>
  <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.x" />
</ItemGroup>

此外,如果您的数据库层是一个单独的项目,您的 add migration 命令也需要包含您的 Startup 项目。 dotnet ef 命令必须从数据库项目路径执行,它应该是这样的:

dotnet ef --startup-project ..\Path\To\Startup migrations add Initial -c YourDatabaseContext

其中启动项目文件还需要包含DotNetCliToolReference 引用。

我没有看到你提到数据库上下文,但我相信你一定有一个。

如果您切换到 .net core 版本 2.1.0,一切都保持不变,除了 CLI 工具现在是核心实体包的一部分,因此您可以从 csproj 文件中删除 CLI 引用。

希望对你有帮助

【讨论】:

  • 数据库上下文在那里。我在根目录中创建了一个名为“Data”的文件夹,它具有上下文文件。模型也在同一个文件夹中,作为子目录。目前这是一个单一的项目。今晚下班后我会做这些步骤。
  • 添加了DotNetCliToolReference,然后执行dotnet restore。之后执行dotnet ef migrations add "Initial" -o "Data\Migrations",这次成功了。除了这个答案,以下也有帮助:github.com/aspnet/EntityFrameworkCore/issues/8996,检查AliBayatGH 的评论To install this package, add it to the DotNetCliToolReference collection in the .csproj file, You have to install this package by editing the .csproj file; you can't use the install-package command or the package manager GUI.
猜你喜欢
  • 2019-02-25
  • 1970-01-01
  • 2013-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-23
  • 1970-01-01
相关资源
最近更新 更多