使用ASP.NET Core工具创建迁移脚本时未找到迁移

时间:2016-09-22 18:38:50

标签: asp.net-core entity-framework-core ef-migrations

我正在尝试使用命令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:迁移'来自'没找到。

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
  3. 我是否正确使用此命令dotnet ef migrations script

    修改1

    我做了一个简单的例子来证明我的问题。该示例可以从这里下载Download example

    我做了什么:

    1. 创建了默认的Web应用程序项目
    2. dotnet ef migrations add Initial
    3. dotnet ef migrations script -o initial.sql - 创建了initial.sql文件
    4. dotnet ef database update
    5. Student
    6. 中添加了Data课程
    7. 添加了``DbSet in ApplicationDbContext```
    8. dotnet ef migrations add Student
    9. dotnet ef migrations script from 20160924174811_Initial -o student.sql
    10. 这里我收到以下错误 - 来自&#39;的迁移&#39;没找到。

      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
      

1 个答案:

答案 0 :(得分:1)

好的我明白了。

from不是选项名称,其位置应直接来自迁移名称。

下面的工作命令示例:

dotnet ef migrations script Initial -o student.sql

或也有效

dotnet ef migrations script 20160924174811_Initial -o student2.sql