161

I'm building a MVC application with .Net Core and I need to generate the script of a migration.

With EF6 I did run the command

update-database -script

but when I try to do the same with .net Core is throwing the next exception:

Update-Database : A parameter cannot be found that matches parameter name 'script'

Do you know if there is an equivalent for EF Core?

Gabriel Castillo Prada
  • 4,114
  • 4
  • 21
  • 31

6 Answers6

225

As per EF documentation you can use :

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.

Mehdiway
  • 10,337
  • 8
  • 36
  • 68
Gasper
  • 2,752
  • 1
  • 12
  • 16
  • 2
    The script-migration tool doesn't seem to work for downgrades (when the To migration is older than the From migration). Any ideas on how to generate an 'undo' script? – Nullius Jan 04 '17 at 16:50
  • 1
    @Nullius you don't want to call it, or see it like its an "undoScript" you just need to make your undo on your code and then update Db and get the newest scripts... – Haithem KAROUI Apr 13 '17 at 14:15
  • 1
    We have a deploy server which also manages db-migrations. E.g.: when a deploy gets rollbacked, also the database should be downgraded to the previous state. The build step that manages the db-migrations, needs an 'up' and 'down' script. We've found out that script-migration also works when specifying an older To migration than the From migration. However, one should set the To migration to the one BEFORE the migration you actually want to migrate. Also, when trying to generate a 'down' script for the latest migration, one should use 'ZZZZZZ' (or similar) for the From migration parameter. – Nullius Apr 14 '17 at 09:43
  • i'm facing a "build failed message when running the command: Script-Migration -From "20170425182604_InitialCreate" -To "20170426023421_AddTableEstoque" :( – rodrigorf May 11 '17 at 17:33
  • Sorry, it seems that a small problem with my csproj build was causing the error. I had to chech the build output tab in VS to get the error details. Thanks for the command, it solved for me – rodrigorf May 11 '17 at 17:40
  • 4
    What if i'm trying to generate script for **First** migration. There is no **PreviousMigration** – tchelidze Sep 24 '18 at 10:45
  • 3
    @tchelidze To script the initial migration, set the -From parameter to 0. e.g. Script-Migration -From 0 – TallMcPaul Jan 15 '19 at 12:20
  • 1
    Example: script-migration -from '20201016160521_InitialCreate' -to '20201016160816_Addrecordstatus' – Mohammad Atiour Islam Oct 16 '20 at 16:10
  • 2
    This is ok for .Net 5 and .Net 6 , I tried this – keivan kashani Dec 12 '21 at 08:35
  • one thing is the From migration is not included in script it is the one after – Mark Homer Sep 11 '22 at 10:27
  • @Gasper: How to generate a migration script if the there are no previous migration present? – Dev-lop-er Oct 20 '22 at 16:54
  • Tip: use -i (--idempotent) to generate a script which will work on a database at any migration. It works by enclosing each migration script in an IF statement which references the __EFMigrationsHistory table. – Tor Haugen Nov 17 '22 at 15:22
  • One thing I've noticed is that the project requires a rebuild after creating a new migration before the script can be generated to that migration. Otherwise you get a 'The migration 'foobar' was not found' message. – phillyd Jun 27 '23 at 08:47
54
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

AttackingMilo
  • 541
  • 4
  • 2
  • For anyone else that got confused why the arguments were missing, make sure that you use `dotnet ef migrations script` and not `dotnet ef dbcontext script` – somethingRandom Nov 25 '22 at 09:24
37

You can use dotnet core cli to generate script

dotnet ef migrations script 

Also you can put this to file with new power shell out-file command.

dotnet ef migrations script | out-file ./script.sql
Ahmar
  • 3,717
  • 2
  • 24
  • 42
19

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 

Afterwards be sure to Remove-Migration to remove the bad migration

Remove-Migration

This works in .Net Core 2.2.0

Mike
  • 331
  • 2
  • 5
9

This also generates only the SQL

Update-Database -script -TargetMigration TO -SourceMigration FROM
Vaibhav Garg
  • 3,630
  • 3
  • 33
  • 55
0

The following generates a SQL script from a blank database to the latest migration.

In .Net Cli type the following.

dotnet ef migrations script

If you want to do the same in visual studio package manager console. Type the following

Script-Migration

The following generates a SQL script from the given migration to the latest migration.

In .Net Cli

dotnet ef migrations script AddNewTables

In visual studio package manager console

Script-Migration AddNewTables

The following generates a SQL script from the specified from migration to the specified to migration.

In .Net Cli

dotnet ef migrations script AddNewTables AddAuditTable

In visual studio package manager console

Script-Migration AddNewTables AddAuditTable

For more details. Please refer to the link.

Lakshitha Kanchana
  • 844
  • 2
  • 12
  • 32