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

时间:2016-07-11 12:21:24

标签: 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

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

2 个答案:

答案 0 :(得分:7)

正如评论中提到的一些人,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)中执行命令。如果您尝试从解决方案根目录执行此操作,则无法正常工作。

答案 1 :(得分:1)

我最终得到了这样的包配置

"dependencies": {
  "Microsoft.NETCore.App": {
    "version": "1.0.1",
    "type": "platform"
  },
  "Microsoft.EntityFrameworkCore.Design": "1.0.1",
  "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.1",
  ...
}

"tools": {
  "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview3-final",
  "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview3-final"
},

并且此命令有效

Scaffold-DbContext
    "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=<DB name>;Integrated Security=True;"
    "Microsoft.EntityFrameworkCore.SqlServer"
    -OutputDir Entities
    -Context "ApplicationDbContext"
    -DataAnnotations
    -Force
    -Project <Project name>.Data
    -StartupProject <Project name>.Api

在这里,我将一个上下文绑定到一个名为Data的单独的类库项目中,其中Web应用程序为Api