简体   繁体   English

我可以先使用 EF 代码和 .net core 生成迁移脚本吗

[英]Can I generate script of a migration with EF code first and .net core

I'm building a MVC application with .Net Core and I need to generate the script of a migration.我正在使用 .Net Core 构建 MVC 应用程序,我需要生成迁移脚本。

With EF6 I did run the command使用 EF6 我确实运行了命令

update-database -script

but when I try to do the same with .net Core is throwing the next exception:但是当我尝试对 .net Core 做同样的事情时,会抛出下一个异常:

Update-Database : A parameter cannot be found that matches parameter name 'script'更新数据库:找不到与参数名称“脚本”匹配的参数

Do you know if there is an equivalent for EF Core?您知道 EF Core 是否有等效项吗?

As per EF documentation you can use Script-Migration command.根据EF 文档,您可以使用Script-Migration命令。

If you want to just script all the migrations you can simply call it from Package Manager console like that.如果您只想编写所有迁移的脚本,您可以像这样从包管理器控制台简单地调用它。 If you want to just script the changes from the last migration you can call it like this:如果您只想编写上次迁移的更改脚本,您可以这样调用它:

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

Be sure to check the docs, there're a few more options to the command.请务必查看文档,该命令还有更多选项。

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.

so,you can try所以,你可以试试

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

This works in .Net Core 2.1这适用于 .Net Core 2.1

You can use dotnet core cli to generate script您可以使用 dotnet core cli 生成脚本

dotnet ef migrations script 

Also you can put this to file with new power shell out-file command.您也可以使用新的 power shell out-file命令将其写入文件。

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

You can also generate a script to rollback a migration by reversing the parameters to Script-Migration.您还可以通过将参数反转为 Script-Migration 来生成一个脚本来回滚迁移。 For example, if you have two migrations, BadLatestMigration and GoodPreviousMigration, you can revert to GoodPreviousMigration by using the following command例如,如果您有两个迁移,BadLatestMigration 和 GoodPreviousMigration,您可以使用以下命令恢复到 GoodPreviousMigration

Script-Migration BadLatestMigration GoodPreviousMigration 

Afterwards be sure to Remove-Migration to remove the bad migration之后一定要使用 Remove-Migration 来移除错误的迁移

Remove-Migration

This works in .Net Core 2.2.0这适用于 .Net Core 2.2.0

这也只生成 SQL

Update-Database -script -TargetMigration TO -SourceMigration FROM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM