Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

Can we run SQL script using Code First migrations?

First you need to create a migration. Then in the generated migration file you can write your SQL.

How do I create a migration file in .NET core?

Adding a Migration So, firstly, you need to create a migration. Open the Package Manager Console from the menu Tools -> NuGet Package Manager -> Package Manager Console in Visual Studio and execute the following command to add a migration.


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.


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


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

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


This also generates only the SQL

Update-Database -script -TargetMigration TO -SourceMigration FROM