Add-Migration fails because EntityFrameworkCore.Tools is not installed

别说谁变了你拦得住时间么 提交于 2019-12-04 14:25:35

As a few people mentioned in the comments, Microsoft.EntityFrameworkCore.Tools needs to be added to the tools section of project.json.

I've tested this project.json with the latest (July 2016) versions of EF Core and the tooling packages, and it works:

{
    "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-*"
}

To use it, execute these commands from inside the project folder:

λ 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)

At first, I was getting No executable found matching command "dotnet-ef" until I realized that you have to execute the commands from inside the project folder (src\projectName in my case). If you try to do it from the solution root directory, it won't work.

I ended up with such package configuration

"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"
},

and this command works

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

Here I scaffold a context into a separate class library project with name Data where the web app is Api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!