【问题标题】:EF Core Database First Approach: Scaffold-DbContext is not recognized as the name of a cmdletEF Core 数据库优先方法:Scaffold-DbContext 未被识别为 cmdlet 的名称
【发布时间】:2017-02-04 21:22:56
【问题描述】:

我正在使用 EF Core,我正在尝试从数据库生成模型。我创建了一个单独的 ASP.NET Core Web 应用程序,因为从类库生成它对我不起作用。

我已经成功从数据库生成实体类几次,但突然停止工作。这真的很奇怪,因为我这不是第一次,而且我没有改变任何东西,是什么导致它停止工作?

这是 project.json:

  {
  "dependencies": {
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
    "Microsoft.AspNetCore.Mvc": "1.1.0",
    "Microsoft.AspNetCore.Routing": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.1.0",
    "Microsoft.Extensions.Configuration.Json": "1.1.0",
    "Microsoft.Extensions.Logging": "1.1.0",
    "Microsoft.Extensions.Logging.Console": "1.1.0",
    "Microsoft.Extensions.Logging.Debug": "1.1.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
    "Microsoft.NETCore.App": {
      "version": "1.1.0",
      "type": "platform"
    },
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0"
  },

  "tools": {

    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  }
}

这是命令:

 Scaffold-DbContext "Server= myserver;Database=db;User Id=sa;Password=pass;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir EntityModels 

错误:

 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.
At line:1 char:1
+ Scaffold-DbContext "Server=myserver;Database=db...
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Scaffold-DbContext:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

我重新启动了 Visual Studio,但问题仍然存在。

【问题讨论】:

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


    【解决方案1】:

    dependencies中的版本与tools不同:

    来自dependencies

    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",

    来自tools

    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",

    【讨论】:

    • 我 100% 确定它正在使用这些软件包。我尝试按照您提到的进行更改,但又出现了另一个错误:System.AggregateException: One or more errors occurred. (Attempt by method 'Microsoft.EntityFrameworkCore.Scaffolding.Configuration.Internal.ModelConfiguration.get_EntityConfigurations()' to access method 'Microsoft.EntityFrameworkCore.Metadata.Internal.EntityTypeNameComparer..ctor()' failed.) ---> System.MethodAccessException: Attempt by method 'Microsoft.EntityFrameworkCore.Scaffolding.Configuration.Internal.ModelConfiguration.get_EntityConfigurations()
    • 查看 EF 1.1 发行说明 here 并向下滚动到“升级工具包” - 您可能需要使用 Microsoft.EntityFrameworkCore.Tools.DotNet 工具。
    • 谢谢,但这没有用。但是,同一项目仍在另一台机器上运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 2012-03-29
    • 2019-10-23
    • 2017-03-26
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多