我能否使用EF Code First和.NET Core生成迁移脚本?

166

我正在使用.Net Core构建MVC应用程序,并且需要生成迁移脚本。

在EF6中,我运行了该命令:

update-database -script

但是当我尝试在 .NET Core 中执行同样的操作时,会抛出以下异常:

Update-Database:找不到与参数名称“script”匹配的参数。

你知道 EF Core 是否有相应的替代方法吗?

6个回答

233
根据EF文档,您可以使用:
Script-Migration 

如果您想批量脚本化所有迁移,只需像这样从包管理器控制台调用它即可。如果您只想脚本化自上次迁移以来的更改,可以像这样调用:

Script-Migration -From <PreviousMigration> -To <LastMigration>

记得查阅文档,该命令还有一些其他选项。


2
脚本迁移工具似乎无法用于降级(当目标迁移版本早于源迁移版本时)。有什么想法可以生成“撤销”脚本吗? - Nullius
1
@Nullius,你不需要把它称为“undoScript”,也不要把它看作是“undoScript”,你只需要在代码中撤销操作,然后更新数据库并获取最新的脚本即可。 - Haithem KAROUI
4
如果我试图为“第一次”迁移生成脚本,但是没有“上一个迁移”,该怎么办? - tchelidze
3
要编写初始迁移脚本,请将“-From”参数设置为0。例如:Script-Migration -From 0。 - TallMcPaul
2
这对于 .Net 5 和 .Net 6 是可以的,我已经尝试过了。 - keivan kashani
显示剩余8条评论

57
dotnet ef migrations script --help

Usage: dotnet ef migrations script [arguments] [options]

Arguments:
  <FROM>  The starting migration. Defaults to '0' (the initial database).
  <TO>    The ending migration. Defaults to the last migration.

Options:
  -o|--output <FILE>                     The file to write the result to.
  -i|--idempotent                        Generate a script that can be used on a database at any migration.
  -c|--context <DBCONTEXT>               The DbContext to use.
  -p|--project <PROJECT>                 The project to use.
  -s|--startup-project <PROJECT>         The startup project to use.
  --framework <FRAMEWORK>                The target framework.
  --configuration <CONFIGURATION>        The configuration to use.
  --runtime <RUNTIME_IDENTIFIER>         The runtime to use.
  --msbuildprojectextensionspath <PATH>  The MSBuild project extensions path. Defaults to "obj".
  --no-build                             Don't build the project. Only use this when the build is up-to-date.
  -h|--help                              Show help information
  -v|--verbose                           Show verbose output.
  --no-color                             Don't colorize output.
  --prefix-output                        Prefix output with level.

所以,你可以尝试一下

dotnet ef migrations script ver1 ver2
dotnet ef migrations script ver1 ver2 -o ./script.sql

这适用于 .Net Core 2.1


对于其他人可能会感到困惑为什么参数丢失的情况,请确保您使用的是 dotnet ef migrations script 而不是 dotnet ef dbcontext script - somethingRandom

41

您可以使用dotnet core cli生成脚本

dotnet ef migrations script 

您还可以使用新的PowerShell命令out-file将此内容写入文件。

dotnet ef migrations script | out-file ./script.sql

4
在CMD中,一个好的提示是: dotnet ef migrations script > script.sql该命令将生成一个名为script.sql的SQL脚本文件。 - Tore Aurstad
4
out-file 的新语法将是 --output ./script.sql - Steve Lam

20
您还可以通过反转Script-Migration的参数生成回滚迁移的脚本。例如,如果您有两个迁移文件BadLatestMigration和GoodPreviousMigration,您可以使用以下命令还原到GoodPreviousMigration。

你也可以通过反转Script-Migration的参数生成回滚迁移的脚本。例如,如果你有两个迁移文件BadLatestMigration和GoodPreviousMigration,你可以使用以下命令还原到GoodPreviousMigration。

Script-Migration BadLatestMigration GoodPreviousMigration 

之后一定要运行 Remove-Migration 命令来删除错误的迁移文件

Remove-Migration

这适用于.Net Core 2.2.0


我正在寻找回滚,这个解决了我的问题。在EF Core 3.0中有效。 - Sehab

9

这也仅生成SQL。

Update-Database -script -TargetMigration TO -SourceMigration FROM

10
这适用于EF5,我不认为它适用于EF6或EF Core。 - Yuval Perelman
4
这适用于EF6。 - Kai Hartmann
他们不断地改变语法!这真是一团糟... 没有任何向后兼容性... - Alexander

1
以下是从空数据库生成SQL脚本到最新迁移的步骤。
.Net Cli中输入以下命令。
dotnet ef migrations script

如果你想在Visual Studio的包管理器控制台中执行相同操作,请输入以下内容。
Script-Migration

以下是根据给定的迁移生成到最新迁移的 SQL 脚本。
在 .Net Cli 中,可以使用以下步骤:
dotnet ef migrations script AddNewTables

在Visual Studio的包管理器控制台中
Script-Migration AddNewTables

以下代码根据指定的起始迁移和目标迁移生成一个 SQL 脚本。 在 .Net Cli 中
dotnet ef migrations script AddNewTables AddAuditTable

在Visual Studio的包管理器控制台中
Script-Migration AddNewTables AddAuditTable

更多详细信息,请参考链接。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接