Generate full SQL script from EF 5 Code First Migrations

Entity FrameworkEntity Framework-Migrations

Entity Framework Problem Overview


How do I use Entity Framework 5 Code First Migrations to create a full database script from the initial (empty) state to the latest migration?

The blog post at MSDN Blog suggests to do this, but it seems to create an empty script:

Update-Database -Script -SourceMigration: $InitialDatabase

Entity Framework Solutions


Solution 1 - Entity Framework

The API appears to have changed (or at least, it doesn't work for me).

Running the following in the Package Manager Console works as expected:

Update-Database -Script -SourceMigration:0

Solution 2 - Entity Framework

For anyone using entity framework core ending up here. This is how you do it.

# Powershell / Package manager console
Script-Migration

# Cli 
dotnet ef migrations script

You can use the -From and -To parameter to generate an update script to update a database to a specific version.

Script-Migration -From 20190101011200_Initial-Migration -To 20190101021200_Migration-2

https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/#generate-sql-scripts

> There are several options to this command. > > The from migration should be the last migration applied to the database before running the script. If no migrations have been applied, specify 0 (this is the default). > > The to migration is the last migration that will be applied to the database after running the script. This defaults to the last migration in your project. > > An idempotent script can optionally be generated. This script only applies migrations if they haven't already been applied to the database. This is useful if you don't exactly know what the last migration applied to the database was or if you are deploying to multiple databases that may each be at a different migration.

Solution 3 - Entity Framework

To add to Matt wilson's answer I had a bunch of code-first entity classes but no database as I hadn't taken a backup. So I did the following on my Entity Framework project:

Open Package Manager console in Visual Studio and type the following:

Enable-Migrations

Add-Migration

Give your migration a name such as 'Initial' and then create the migration. Finally type the following:

Update-Database

Update-Database -Script -SourceMigration:0

The final command will create your database tables from your entity classes (provided your entity classes are well formed).

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
QuestionMatt WilsonView Question on Stackoverflow
Solution 1 - Entity FrameworkMatt WilsonView Answer on Stackoverflow
Solution 2 - Entity FrameworkJustin LessardView Answer on Stackoverflow
Solution 3 - Entity FrameworkTrevorView Answer on Stackoverflow