【问题标题】:Can I generate script of a migration with EF code first and .net core我可以先使用 EF 代码和 .net 核心生成迁移脚本吗
【发布时间】:2017-01-31 08:33:02
【问题描述】:

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

使用 EF6 我确实运行了命令

update-database -script

但是当我尝试对 .net Core 做同样的事情时,会抛出下一个异常:

更新数据库:找不到与参数匹配的参数 名称'脚本'

你知道是否有 EF Core 的等价物吗?

【问题讨论】:

    标签: .net entity-framework asp.net-core asp.net-core-mvc entity-framework-core


    【解决方案1】:

    根据EF documentation,您可以使用Script-Migration 命令。

    如果您只想编写所有迁移的脚本,您可以像这样从包管理器控制台调用它。如果您只想编写上次迁移的更改脚本,可以这样调用它:

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

    请务必查看文档,该命令还有更多选项。

    【讨论】:

    • 脚本迁移工具似乎不适用于降级(当 To 迁移早于 From 迁移时)。关于如何生成“撤消”脚本的任何想法?
    • @Nullius 您不想调用它,或者将其视为“撤消脚本”,您只需对代码进行撤消,然后更新 Db 并获取最新脚本...
    • 如果我尝试为 First 迁移生成脚本怎么办。没有PreviousMigration
    • @tchelidze 要编写初始迁移脚本,请将 -From 参数设置为 0。例如脚本迁移 - 从 0
    • .Net 5 和 .Net 6 都可以,我试过了
    【解决方案2】:
    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

    【讨论】:

      【解决方案3】:

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

      dotnet ef migrations script 
      

      您也可以使用新的 power shell out-file 命令将其放入文件中。

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

      【讨论】:

      • 好的提示,或者在 CMD 中:dotnet ef migrations script > script.sql
      • out-file 的新语法将是 --output ./script.sql
      【解决方案4】:

      您还可以通过将参数反转为 Script-Migration 来生成脚本以回滚迁移。例如,如果您有两个迁移,BadLatestMigration 和 GoodPreviousMigration,您可以使用以下命令恢复到 GoodPreviousMigration

      Script-Migration BadLatestMigration GoodPreviousMigration 
      

      之后一定要使用 Remove-Migration 来移除不良迁移

      Remove-Migration
      

      这适用于 .Net Core 2.2.0

      【讨论】:

      • 我正在寻找回滚,这为我解决了它。适用于 EF Core 3.0
      【解决方案5】:

      这也只生成 SQL

      Update-Database -script -TargetMigration TO -SourceMigration FROM
      

      【讨论】:

      • 这适用于 EF5,我认为它不适用于 EF6 或 EF 核心
      • 这适用于 EF6。
      猜你喜欢
      • 1970-01-01
      • 2015-11-14
      • 2017-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-19
      • 2016-03-18
      相关资源
      最近更新 更多