EF Core:Scaffold DbContext一直都失败了

时间:2016-10-02 12:58:27

标签: c# asp.net-mvc entity-framework

官方ASP.Net Core表示可以通过重新启动visual studio来修复以下错误:

 Scaffold-DbContext : The term 'Scaffold-DbContext' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

然而,该命令仍然失败。

我按照ASP.Net核心在线文档中提供的示例在Package Manager控制台中运行它。

Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。对我来说,它是缺少EntityFrameworkCore.Tools并且没有通过NuGet正确安装。

在包管理器控制台(Tools > NuGet Package Manager > Package Manager Console)中运行这些命令:

Install-Package Microsoft.EntityFrameworkCore.SqlServer –Pre 
Install-Package Microsoft.EntityFrameworkCore.Tools –Pre 
Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design –Pre

然后在 project.json 中我添加了这个:

"tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
}

重要! 确保此版本符合"依赖项" 包(也在project.json中)。在此示例中," 1.1.0-preview4-final" 也应该用于依赖项。

如果问题仍然存在,请尝试恢复您的ef运行时,在此处找到指导:https://github.com/aspnet/EntityFramework/issues/5549

答案 1 :(得分:2)

这是我在创建新的ASP.NET Core 1.1项目时开始的,这节省了我很多时间。如果您完成步骤1并且步骤2无法立即生效,请尝试重新启动Visual Studio并继续执行步骤2.

第1步

将以下内容用作project.json。恢复包。

{
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore": "1.1.0",
    "Microsoft.EntityFrameworkCore.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
    "NETStandard.Library": "1.6.1",
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.1.0"
    }
  },
  "frameworks": {
    "netcoreapp1.1": {
      "imports": "dnxcore50"
    }
  },
  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final",
    "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.1.0-preview4-final"
  }
}

第2步

在程序包管理器控制台中运行查询。下面的查询连接到TestServer中的TestDb数据库

Scaffold-DbContext "Data Source=TestServer;Initial Catalog=TestDb;Persist Security Info=True;User ID={Username};Password={Password}" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -force -v -t dbo.Table1, dbo.Table2, dbo.Table3