使用ASP.NET Core工具創建遷移腳本時,無法進行遷移

asp.net-core ef-migrations entity-framework-core

我正在嘗試使用命令dotnet ef migrations script ,以便在上次應用的遷移和最後一次未應用的遷移之間創建SQL腳本。應用遷移是一種稱為dotnet ef database update遷移

我嘗試執行以下命令,但它們都不起作用:

dotnet ef  --startup-project ../DbMigrations/ migrations script from LastAppliedMigration to LastNotAppliedMigration -o temp.sql --context MyProject.DbModel.MusicDbContext

dotnet ef  --startup-project ../DbMigrations/ migrations script from LastAppliedMigration -o temp.sql --context MyProject.DbModel.MusicDbContext

我收到以下錯誤: System.InvalidOperationException:找不到遷移'from'。

System.InvalidOperationException: The migration 'from' was not found.
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsAssemblyExtensions.GetMigrationId(IMigrationsAssembly assembly, String nameOrId)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateScript(String fromMigration, String toMigration, Boolean idempotent)
   at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.ScriptMigration(String fromMigration, String toMigration, Boolean idempotent, String contextType)
   at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsScriptCommand.Execute(CommonOptions commonOptions, String from, String to, String output, Boolean idempotent, String context, String environment)
   at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsScriptCommand.<>c__DisplayClass0_0.<Configure>b__0()
   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)

對於遷移名稱,我嘗試了兩個版本的前綴timestamp並且沒有:

  1. 20160920085915_LastAppliedMigration
  2. LastAppliedMigration

我是否正確使用此命令dotnet ef migrations script

編輯1

我做了一個簡單的例子來證明我所遇到的問題。該示例可以從此處下載下載示例

我做了什麼:

  1. 創建了一個默認的Web應用程序項目
  2. dotnet ef migrations add Initial
  3. dotnet ef migrations script -o initial.sql - 創建了initial.sql文件
  4. dotnet ef database update
  5. Data添加了Student
  6. in ApplicationDbContext
中添加了``DbSet
  • dotnet ef migrations add Student
  • dotnet ef migrations script from 20160924174811_Initial -o student.sql
  • 在這裡我收到以下錯誤 - 找不到遷移'from'。

    Project WebApplication3 (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.              
    System.InvalidOperationException: The migration 'from' was not found.                                          
       at Microsoft.EntityFrameworkCore.Migrations.MigrationsAssemblyExtensions.GetMigrationId(IMigrationsAssembly assembly, String nameOrId)                                                                                     
       at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateScript(String fromMigration, String to Migration, Boolean idempotent)                                                                                 
       at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.ScriptMigration(String fromMigration, String to Migration, Boolean idempotent, String contextType)                                                             
       at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsScriptCommand.Execute(CommonOptions commonOptions, String from, String to, String output, Boolean idempotent, String context, String environment)                     
       at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsScriptCommand.<>c__DisplayClass0_0.<Configure>b__0()   
       at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)                      
       at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)                                      
    The migration 'from' was not found.
    

    當我做dotnet ef migrations list我得到以下結果:

    Project WebApplication3 (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
    20160924174811_Initial
    20160924175511_Student
    

    CLI工具的版本是

    dotnet --version
    1.0.0-preview2-003131
    

    熱門答案

    好吧我明白了。

    from不是選項名稱,它的位置應直接來​​自遷移名稱。

    下面的工作命令示例:

    dotnet ef migrations script Initial -o student.sql

    或者也是有效的

    dotnet ef migrations script 20160924174811_Initial -o student2.sql



    Related

    許可下: CC-BY-SA with attribution
    不隸屬於 Stack Overflow
    許可下: CC-BY-SA with attribution
    不隸屬於 Stack Overflow