Get SQL file for specific migration in Entity Framework 6 C#

ef-code-first entity-framework entity-framework-6

Question

In Entity Framework 6, I need to get the SQL script for specific migration file and I already updated the database. I found that in update-database command I can add script parameter to export the migration to SQL, but I already updated the database. I found in Entity Framework Core, a command called Script-Migration with script name as an argument. Is there any similar command in Entity framework?

I also tried the suggestion "Update database command", but I got an empty SQL file.

1
10
2/12/2020 8:32:22 PM

Accepted Answer

Yes, you could generate the migration-SQL as follows:

Update-Database -Script -SourceMigration: <pointFromWichYouWantToStartWithGeneration> -TargetMigration: <pointWhereToEndWithGeneration>

To create a script for all migrations execute the following:

Update-Database -Script -SourceMigration: $InitialDatabase

Instead of applying the changes to database it will generate a SQL script file. Therefore you could generate a SQL Script even if the migration was already applied to the database.

Here you could find some more information about it - Entity Framework Code First Migrations - Getting a SQL Script.

Run the Update-Database command but this time specify the –Script flag so that changes are written to a script rather than applied. We’ll also specify a source and target migration to generate the script for.

19
2/12/2020 8:34:00 PM

Popular Answer

If you're now in .net core use

Script-Migration -From <PreviousMigration> -To <LastMigration>


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow