我可以先使用 EF 代码和 .net core 生成迁移脚本吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39644544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I generate script of a migration with EF code first and .net core
提问by Gabriel Castillo Prada
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 EF7?
你知道EF7是否有等价物吗?
回答by Gasper
As per EF documentationyou can use Script-Migrationcommand.
根据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.
请务必查看文档,该命令还有更多选项。
回答by Ahmar
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-filecommand.
您也可以使用新的 power shellout-file命令将其写入文件。
dotnet ef migrations script | out-file ./script.sql
回答by Mike
You can also generate a script to rollback a migration by reversing the parameters to Script-Migration. For example, if you have two migrations, BadLatestMigration and GoodPreviousMigration, you can revert to GoodPreviousMigration by using the following command
您还可以通过将参数反转为 Script-Migration 来生成一个脚本来回滚迁移。例如,如果您有两个迁移,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
回答by AttackingMilo
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
回答by Vaibhav Garg
This also generates only the SQL
这也只生成 SQL
Update-Database -script -TargetMigration TO -SourceMigration FROM

