.NET实体框架核心

3

我已经阅读了过去两年中发布的与EF相关的几乎所有问题。 我没有遇到下载其他软件包的任何问题,只有Entity Framework无法安装。

我甚至尝试安装最新版本的Nuget,并在项目目录中使用其工具来还原软件包 - 在我添加此行到我的.csproj文件之后:

<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.2" />

首先,我尝试了这个命令:

dotnet restore

生成以下输出:

C:\Path\To\My\Project>dotnet restore
Restore completed in 100.41 ms for C:\Path\To\My\Project\TestNuget.csproj.
Restore completed in 68.84 ms for C:\Path\To\My\Project\TestNuget.csproj.

并导致以下结果:

> dotnet ef
No executable found matching command "dotnet-ef"

因此,我尝试了这个(使用截至今天日期的最新版本的Nuget):

nuget restore Example.csproj

这会输出以下内容:
MSBuild auto-detection: using msbuild version '15.6.85.37198' from 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\bin'.
Committing restore...
Tool assets file has not changed. Skipping assets file write. Path: C:\Path\ToNuget\.nuget\packages\.tools\microsoft.visualstudio.web.codegeneration.tools\2.0.3\netcoreapp2.0\project.assets.json
Restore completed in 139.95 ms for C:\Path\To\User\source\repos\Example\Example\Example.csproj.
Committing restore...
Assets file has not changed. Skipping assets file writing. Path: C:\Path\To\User\source\repos\Example\Example\obj\project.assets.json
Restore completed in 113.51 ms for C:\Path\To\User\source\repos\Example\Example\Example.csproj.

NuGet Config files used:
    C:\Path\To\User\AppData\Roaming\NuGet\NuGet.Config
    C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.Offline.config
    C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.Offline.Fallback.config

Feeds used:
    C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\
    https://api.nuget.org/v3/index.json

这与原来的结果相同。

我不确定问题出在哪里,我在网上阅读的所有内容,包括https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet都指出,你只需要修改.csproj文件并使用dotnet/nuget包管理工具进行恢复,但是这似乎对我的项目毫无作用,而且dotnet-ef.exe仍然无处可寻。


你是在使用Visual Studio的包管理器控制台还是CLI? - Mike
我都试过了。唯一的区别是PMC似乎更冗长。 - Gabriel Alexander Hayes
PMC和CLI是两个不同的环境,使用两个不同的包。 - Mike
1个回答

3
我做了以下操作,它可以正常工作:
1.创建一个新的.NET Core MVC Web应用程序 2.打开命令提示符,将目录更改为您的应用程序项目文件夹 3.执行以下命令
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet restore
在执行以上命令后,在您的.csproj文件中包含以下行(在闭合Project标签之前)。
<ItemGroup>
  <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>

完整的.csproj文件可能如下所示:
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.2" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3" />
  </ItemGroup>
  <ItemGroup>
  <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>

</Project>

输入dotnet ef

如果所有配置都正确,您将看到以下响应

                     _/\__
               ---==/    \\
         ___  ___   |.    \|\
        | __|| __|  |  )   \\\
        | _| | _|   \_/ |  //|\\
        |___||_|       /   \\\/\\

Entity Framework Core .NET Command Line Tools 2.0.0-rtm-26452

Usage: dotnet ef [options] [command]

Options:
  --version        Show version information
  -h|--help        Show help information
  -v|--verbose     Show verbose output.
  --no-color       Don't colorize output.
  --prefix-output  Prefix output with level.

Commands:
  database    Commands to manage the database.
  dbcontext   Commands to manage DbContext types.
  migrations  Commands to manage migrations.

Use "dotnet ef [command] --help" for more information about a command.

这只能在2.0.0中使用吗?编辑:我马上试一下。 - Gabriel Alexander Hayes
哇,我真笨。在更新了.csproj文件之后,我竟然没有构建项目。非常感谢! - Gabriel Alexander Hayes
dotnet restore 不会构建项目,而是还原依赖项和工具。 - Mike

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接