Gabriel Castillo Prada
Gabriel Castillo Prada

Reputation: 4673

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

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?

Upvotes: 179

Views: 230987

Answers (7)

Rishu Ranjan
Rishu Ranjan

Reputation: 574

To generate migration script from EF Core 7. Please follow the below steps.

a. Install EF tool

   dotnet tool install --global dotnet-ef

If your installation fails due to mismatch SDKs, or you get 'attempting to install a preview release' error, specify version.

 dotnet tool install --global dotnet-ef  --version 7.0.2(works in my case)

b. Generate script

dotnet ef migrations script --startup-project <StartUp-ProjectName> --project <ProjectName-Containing-DBContext> -o <OutputFileName>.sql

If you have multiple db context in your project, then you need to mention specific dbcontext

dotnet ef migrations script --startup-project <StartUp-ProjectName> --project <ProjectName-Containing-DBContext> -o <OutputFileName>.sql --context <DBContextName>

If you want to generate Idempotent script, then add idempotent flag

 dotnet ef migrations script --startup-project <StartUp-ProjectName> --project <ProjectName-Containing-DBContext> -o <OutputFileName>.sql --context <DBContextName> --Idempotent 

Note: EF Core generates the scripts based on your migration folder. If your migration folder is not synced with your database, then idempotent scripts will not truly be idempotent. You might get errors if you run scripts manually.

Upvotes: 2

Lakshitha Kanchana
Lakshitha Kanchana

Reputation: 1235

Case 01: 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

Case 02: 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

Case 03: 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.

Upvotes: 3

Gasper
Gasper

Reputation: 3030

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.

Upvotes: 253

AttackingMilo
AttackingMilo

Reputation: 671

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

Upvotes: 67

Vaibhav Garg
Vaibhav Garg

Reputation: 3716

This also generates only the SQL

Update-Database -script -TargetMigration TO -SourceMigration FROM

Upvotes: 9

Mike
Mike

Reputation: 371

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

Upvotes: 23

Ahmar
Ahmar

Reputation: 3867

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

Upvotes: 47

Related Questions