c# - 添加迁移失败,因为未安装 EntityFrameworkCore.Tools

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

我想按照本教程使用 EF Core 创建控制台应用程序:http://ef.readthedocs.io/en/latest/platforms/full-dotnet/new-db.html .

我的问题是,我无法执行语句

Add-Migration 

如教程中所述。它向我展示:

PM> Add-Migration MyFirstMigration
Cannot execute this command because 'Microsoft.EntityFrameworkCore.Tools' is not installed in project 'src\AppEf'. Add 'Microsoft.EntityFrameworkCore.Tools' to the 'tools' section in project.json. See http://go.microsoft.com/fwlink/?LinkId=798221 for more details.

所有添加的程序集:

enter image description here

怎么了?

更新

dotnet restore 语句有效,而 dotnet-ef --help 根本不起作用。
enter image description here

可以看到,语句是在项目文件夹中执行的。

最佳答案

正如一些人在评论中提到的,Microsoft.EntityFrameworkCore.Tools 需要添加到 project.json 的 tools 部分。

我已经使用最新(2016 年 7 月)版本的 EF Core 和工具包测试了这个 project.json,它有效:

{
    "buildOptions": {
    "emitEntryPoint": true
  },
  "dependencies": {
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
    "Microsoft.EntityFrameworkCore.Design": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.0"
    }
  },
  "frameworks": {
    "netcoreapp1.0": {}
  },
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
  },
  "version": "1.0.0-*"
}

要使用它,请从项目文件夹的内部执行这些命令:

λ dotnet restore
log  : Restore completed in 2810ms.

λ dotnet ef --help
Compiling EfCoreTester2 for .NETCoreApp,Version=v1.0                                                                            
Compilation succeeded.                                                                                                          
    0 Warning(s)                                                                                                                
    0 Error(s)                                                                                                                  
Time elapsed 00:00:01.7208394                                                                                                   

Entity Framework .NET Core CLI Commands 1.0.0-preview2-21431
Usage: dotnet ef [options] [command]
Options:
  -h|--help                      Show help information
  -v|--verbose                   Enable verbose output
  --version                      Show version information
(... etc)

起初,我得到的是 No executable found matching command "dotnet-ef" 直到我意识到你必须从项目文件夹 (src\projectName在我的例子中)。如果您尝试从解决方案根目录执行此操作,它将不起作用。

关于c# - 添加迁移失败,因为未安装 EntityFrameworkCore.Tools,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38306791/

相关文章:

entity-framework - 使用 Active Directory 集成身份验证的 Azure SQL 数据库连接无法打开

c# - 为什么数据库数据没有被更新但对象却没有错误?

c# - 如何将 EF 核心迁移添加到 .net 核心 2 类库?

entity-framework - EF core 3 之后 IDENTITY 列的变化

c# - 在 .net core 上创建 EF 迁移时出现 System.IO.FileNotFoundException

c# - .NET 中是否有 "gettimeofday"的等价物

c# - 等待一项任务而不等待其他任务是否可以?

c# - 如何防止 ReSharper 在方法调用中包装 lambda 表达式

asp.net - 合并 IdentityDbContext 和 DbContext ASP.NET MVC

c# - 将两个可选参数传递给 RedirectToRoute