Table of Contents

IT:AD:EF/CodeFirst/Migrations:HowTo:Generate SQL Scripts

Summary

Process

Run the IT:AD:EF/CodeFirst/Migrations:HowTo:Update-Database command, but this time, adding the -script switch so that the changes are output as a sql script, rather than being applied.

Generate a Full Script


Generate an Incremental Script From X to Y

We’ll also specify a source and target migration to generate the script for. We want a script to go from an empty database ($InitialDatabase) to the latest version (migration AddPostAbstract).

Update-Database -startupprojectname:App.AppHost.Web -projectname:App.Infrastructure -Script -SourceMigration: $InitialDatabase -TargetMigration: OurNamedMigration

Generate an Incremental Script from last Applied Migration till Now

Probably the most used scenario:

Update-Database -startupprojectname:App.AppHost.Web -projectname:App.Infrastructure -Script

Note:Omitting SourceMigration means Last Applied In Db, and omitting TargetMigration means Current Code Model.

## Notes ##

$InitialDatabase appears to be a keyword…