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
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
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).
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
https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/#generate-sql-scripts
来源:https://stackoverflow.com/questions/13939404/generate-full-sql-script-from-ef-5-code-first-migrations