Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法识别Visual Studio代码实体框架核心添加迁移_C#_Visual Studio Code_Entity Framework Core - Fatal编程技术网

C# 无法识别Visual Studio代码实体框架核心添加迁移

C# 无法识别Visual Studio代码实体框架核心添加迁移,c#,visual-studio-code,entity-framework-core,C#,Visual Studio Code,Entity Framework Core,我使用yoman通过Visual Studio代码编辑器生成了一个ASP.Net核心Web API应用程序。作为参考,我遵循了本教程 API运行良好。但是,我尝试将实体框架核心迁移与SQL Server一起使用。在Visual Studio代码终端中键入以下内容时: Add-Migration MyDbInitialMigration 我得到以下信息: 'Add-Migration' is not recognized as an internal or external command, o

我使用yoman通过Visual Studio代码编辑器生成了一个ASP.Net核心Web API应用程序。作为参考,我遵循了本教程

API运行良好。但是,我尝试将实体框架核心迁移与SQL Server一起使用。在Visual Studio代码终端中键入以下内容时:

Add-Migration MyDbInitialMigration
我得到以下信息:

'Add-Migration' is not recognized as an internal or external command, operable program or batch file.
我已经安装了Microsoft.EntityFrameworkCore.Tools:1.1.0-preview4-final依赖项。我使用.Net核心项目管理器(Nuget)扩展实现了这一点

在Visual Studio 2015中,此命令在Package Manager控制台中运行良好

我假设使用VisualStudio代码的终端就是问题所在。但是有人知道我如何在VS代码编辑器中使用EF核心迁移吗

谢谢你的帮助

解决方案

运行
dotnet ef migrations add InitialCreate
命令产生以下错误:

No executable found matching command "dotnet-ef"
为了解决这个问题,我需要安装以下依赖项,并将其添加到工具部分:

Microsoft.EntityFrameworkCore.Tools.DotNet

添加新迁移的正确格式是
dotnet ef migrations add yourMigrationName

更新数据库是第1步 首先,我们需要以以下方式在*.csproj文件中添加引用

Bash/Command-propt中的步骤2

网络还原

步骤3

dotnet ef迁移添加MyDbInitialMigration


我在Mac上工作,所以默认安装Ruby。我的EF命令需要很多额外的参数
--project
--startup project
等等。每次键入这些参数都很麻烦,所以我使用rake来简化这一过程

在我的项目根目录中,我添加了一个名为
rakefile
的文件,其中包含以下内容:

desc "Add Migraion"
task :'add-migration' do
    ARGV.each { |a| task a.to_sym do ; end }  
    puts ARGV[1]
    sh "dotnet ef migrations add " + ARGV[1] + " --project MyProject.Data/MyProject.Data.csproj --startup-project MyProject.Web/MyProject.Web.csproj "
end

desc "Remove Migraion"
task :'remove-migration' do
    ARGV.each { |a| task a.to_sym do ; end }  
    puts ARGV[1]
    sh "dotnet ef migrations remove --project MyProject.Data/MyProject.Data.csproj --startup-project MyProject.Web/MyProject.Web.csproj"
end

desc "Update Database"
task :'update-database' do
    ARGV.each { |a| task a.to_sym do ; end }  
    puts ARGV[1]
    sh "dotnet ef database update --project MyProject.Data/MyProject.Data.csproj --startup-project MyProject.Web/MyProject.Web.csproj"
end
然后在命令行中,我运行以下命令:

rake add-migration <migrationName>
rake remove-migration
rake update-database
rake添加迁移
耙移迁移
rake更新数据库

在这种情况下,必须首先检查项目。如果项目中存在任何错误,则无法启动迁移。

您需要添加:

dotnet tool install --global dotnet-ef

为了加深您的怀疑,您能否确认在控制台中同样不起作用?还请发布完整的
project.json
或包文件。在控制台中,您应该使用以下命令:dotnet ef migrations add MyDbInitialMigration。我认为VS终端也应该如此。谢谢你-根据你的回答找到了解释这一点的官方文档。希望你仍然可以看到这条评论。。。在使用dotnet CLI执行此操作的新方法中,我很难找到官方文档。实际上,在添加迁移失败后,我来到了这个StackOverflow页面。介意分享一下您在文档中的何处找到的信息吗?对于那些想查看官方文档的人:但是对于有许多项目的解决方案,如何指定一个项目在其上进行添加迁移或更新数据库?在运行命令之前,从终端窗口,将cd放入项目根文件夹中。新的Migrations文件夹将显示在该项目文件夹中,其中包含预期的所有迁移工件。以下是指向MS docs的更新链接:在Microsoft.EntityFrameworkCore.Tools.Dotnet的.csproj文件中添加引用您可以使用帖子下的编辑链接进行更新。