Can I generate script of a migration with EF code first and .net core

.NetEntity Frameworkasp.net Coreasp.net Core-MvcEntity Framework-Core

.Net Problem Overview


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?

.Net Solutions


Solution 1 - .Net

As per EF documentation you can use Script-Migration command.

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.

Solution 2 - .Net

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

Solution 3 - .Net

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

Solution 4 - .Net

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

Solution 5 - .Net

This also generates only the SQL

Update-Database -script -TargetMigration TO -SourceMigration FROM

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGabriel Castillo PradaView Question on Stackoverflow
Solution 1 - .NetGasperView Answer on Stackoverflow
Solution 2 - .NetAttackingMiloView Answer on Stackoverflow
Solution 3 - .NetAhmarView Answer on Stackoverflow
Solution 4 - .NetMikeView Answer on Stackoverflow
Solution 5 - .NetVaibhav GargView Answer on Stackoverflow