我可以首先使用EF代码和.net核心生成迁移脚本

Gab*_*ada 50 .net entity-framework entity-framework-core asp.net-core-mvc asp.net-core

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

使用EF6,我确实运行了命令

update-database -script
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用.net时也是如此.

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

你知道EF7是否有相同的产品吗?

小智 82

根据EF文档,您可以使用Script-Migration命令.

如果您只想编写所有迁移的脚本,只需从Package Manager控制台中调用它即可.如果您只想编写上次迁移的更改脚本,可以像下面这样调用它:

Script-Migration -From <PreviousMigration> -To <LastMigration>
Run Code Online (Sandbox Code Playgroud)

一定要检查文档,这个命令还有一些选项.

  • 如果我尝试为 **First** 迁移生成脚本怎么办。没有 **PreviousMigration** (4认同)
  • 这对于 .Net 5 和 .Net 6 来说没问题,我试过了 (3认同)
  • 脚本迁移工具似乎不适用于降级(当“目标”迁移早于“源”迁移时)。关于如何生成“撤消”脚本有什么想法吗? (2认同)
  • @tchelidze 要编写初始迁移脚本,请将 -From 参数设置为 0。例如 Script-Migration -From 0 (2认同)

小智 26

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.
Run Code Online (Sandbox Code Playgroud)

所以,你可以试试

dotnet ef migrations script ver1 ver2
dotnet ef migrations script ver1 ver2 -o ./script.sql
Run Code Online (Sandbox Code Playgroud)

这适用于 .Net Core 2.1


Ahm*_*mar 12

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

dotnet ef migrations script 
Run Code Online (Sandbox Code Playgroud)

您也可以使用新的power shell out-file命令将其置于文件中.

dotnet ef migrations script | out-file ./script.sql
Run Code Online (Sandbox Code Playgroud)

  • 好提示,或者在 CMD 中:dotnet ef 迁移脚本 &gt; script.sql (4认同)
  • `out-file` 的新语法将是 `--output ./script.sql` (2认同)

Mik*_*ike 7

您还可以通过将参数反转为“脚本迁移”来生成脚本以回滚迁移。例如,如果您有两个迁移BadLatestMigration和GoodPreviousMigration,则可以使用以下命令还原为GoodPreviousMigration

Script-Migration BadLatestMigration GoodPreviousMigration 
Run Code Online (Sandbox Code Playgroud)

之后,请确保删除-迁移以删除错误的迁移

Remove-Migration
Run Code Online (Sandbox Code Playgroud)

这适用于.Net Core 2.2.0


Vai*_*arg 6

这也只生成 SQL

Update-Database -script -TargetMigration TO -SourceMigration FROM
Run Code Online (Sandbox Code Playgroud)

  • 这对于 EF5 是正确的,我认为它不适用于 EF6 或 EF 核心 (8认同)
  • 这适用于 EF6。 (4认同)