我可以先用EF代码和.net内核生成迁移脚本吗
我正在用.Net内核构建一个MVC应用程序,我需要生成迁移脚本 使用EF6,我确实运行了命令我可以先用EF代码和.net内核生成迁移脚本吗,.net,entity-framework,asp.net-core,asp.net-core-mvc,entity-framework-core,.net,Entity Framework,Asp.net Core,Asp.net Core Mvc,Entity Framework Core,我正在用.Net内核构建一个MVC应用程序,我需要生成迁移脚本 使用EF6,我确实运行了命令 update-database -script 但当我尝试对.net Core执行相同操作时,会引发下一个异常: 更新数据库:找不到与参数匹配的参数 名称“脚本” 您知道EF Core是否有等效版本吗?根据,您可以使用脚本迁移命令 如果您只想编写所有迁移的脚本,您可以像这样从PackageManager控制台调用它。如果只想编写上一次迁移的更改脚本,可以这样调用: Script-Migration -
update-database -script
但当我尝试对.net Core执行相同操作时,会引发下一个异常:
更新数据库:找不到与参数匹配的参数
名称“脚本”
您知道EF Core是否有等效版本吗?根据,您可以使用脚本迁移
命令
如果您只想编写所有迁移的脚本,您可以像这样从PackageManager控制台调用它。如果只想编写上一次迁移的更改脚本,可以这样调用:
Script-Migration -From <PreviousMigration> -To <LastMigration>
脚本迁移-从-到
请务必检查文档,该命令还有几个选项。您可以使用dotnet core cli生成脚本
dotnet ef migrations script
您还可以使用新的powershellout file
命令将其放入文件
dotnet ef migrations script | out-file ./script.sql
您还可以通过将参数反转为脚本迁移来生成用于回滚迁移的脚本。例如,如果有两个迁移,BadLatestMigration和GoodPreviousMigration,则可以使用以下命令还原到GoodPreviousMigration
Script-Migration BadLatestMigration GoodPreviousMigration
之后,请确保删除迁移以删除错误迁移
Remove-Migration
这在.Net Core 2.2.0中起作用,它也只生成SQL
Update-Database -script -TargetMigration TO -SourceMigration FROM
这适用于.Net Core 2.1脚本迁移工具似乎不适用于降级(当to迁移早于From迁移时)。有没有关于如何生成“撤消”脚本的想法?@Nullius您不想调用它,或者将其视为“撤消脚本”您只需要对代码进行撤消,然后更新数据库并获取最新脚本……我们有一个部署服务器,它还管理数据库迁移。例如:当部署被回滚时,数据库也应该降级到以前的状态。管理数据库迁移的构建步骤需要“向上”和“向下”脚本。我们发现,当指定比“从”迁移更旧的“到”迁移时,脚本迁移也可以工作。但是,应该将“要迁移”设置为实际要迁移的迁移之前的值。此外,当尝试为最新的迁移生成“向下”脚本时,应该对From migration参数使用“zzzz”(或类似名称)。如果我尝试为第一次迁移生成脚本,该怎么办。没有PreviousMigration@tchelidze要编写初始迁移脚本,请将-From参数设置为0。e、 g.脚本迁移-从0这对EF5是正确的,我认为它在EF6或EF Core上不起作用。我正在寻找回滚,这为我解决了这个问题。在EF Core 3.0Good tip或CMD:dotnet EF migrations script>script.sqlout file的新语法将是
--output./script.sql
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