By default when using Entity Framework Migrations, the Update-Database PowerShell script will immediately apply your changes to your target databases. But what if you just want to generate a SQL script (to give it to your DBA for example)?
To do this run the Update-Database command in the Package Manager Console but this time specify the –Script flag so that changes are written to a script rather than applied. Also specify a source and target migration to generate the script for.
Update-Database -Script -SourceMigration: $InitialDatabase
- If you don’t specify a target migration, Migrations will use the latest migration as the target.
- If you don't specify a source migrations, Migrations will use the current state of the database.
- If you want to create a script from the initial state to the latest migration, use $InitialDatabase as the SourceMigration value.
Code First Migrations will run the migration pipeline but instead of actually applying the changes it will write them out to a .sql file for you. Once the script is generated, it is opened for you in Visual Studio: