c# - .NET Entity Framework 核心

标签 c# entity-framework .net-core entity-framework-core csproj

我已经通读了过去 2 年中发布的关于 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 仍然找不到.

最佳答案

我做了以下并且有效,

  1. 创建一个新的 .NET Core MVC Web 应用
  2. 转到命令提示符,更改 dir 到您的应用程序项目文件夹
  3. 执行以下命令
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet restore

以上命令执行后, 在您的 .csproj 文件中包含以下行(就在结束项目标记之前)

<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.

关于c# - .NET Entity Framework 核心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49714468/

相关文章:

c# - AutoFac/.NET Core - 注册 DBcontext

javascript - 请求总是被 CORS 策略阻止 c# net core

c# - 有什么理由不使用 `new object().foo()` 吗?

c# - 如何将嵌入式数据库与 Entity Framework 结合使用?

c# - 基于foreach循环选择学生记录

entity-framework - 如何首先在 EF 4.1 代码中使用延迟加载和两个表上的相同主键编写可选的一对一关系?

.net-core - 将 xUnit 项目升级到 .NET Core 2.1.1

c# - 无法使用 C# 中的轨迹栏更改音频位置

c# - Silverlight 中的请求对话框

c# - 为什么在重写的 Object.Equals 方法中抛出 NullReferenceException?