Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Migrations migrate.exe generate script

I'm playing around with Entity framework and continuous builds. So far i'm able to run a migration or series of migrations without any problem by using migrate.exe and the appropriate arguments.

However, i've hit trouble when trying to get migrate.exe to kick out a script, rather than perform the migration, in the same way as I could get by running

update-database -TargetMigration TestMigration -script

from within Package Manager Console in Visual Studio.

Is there currently a way to do this?

Thanks.

like image 474
Greg Smith Avatar asked Jul 21 '12 15:07

Greg Smith


People also ask

Which command generate SQL script from migrations?

With From and To The following generates a SQL script from the specified from migration to the specified to migration. You can use a from that is newer than the to in order to generate a rollback script.

How do I get the migration script in ef core?

Migrations are enabled by default in EF Core. They are managed by executing commands. If you have Visual Studio, you can use the Package Manager Console (PMC) to manage migrations. Alternatively, you can use a command line tool to execute Entity Framework CLI commands to create a migration.


2 Answers

Since the 10/22/2017 you can do it thanks to this PR: https://github.com/aspnet/EntityFramework6/commit/02ec6b8c9279f93f80eeed1234e5ce0acfce5f31

Here the Entity Framework 6.2 release notes that implements this functionality (see 'Migrate.exe should support -script option' section): https://blogs.msdn.microsoft.com/dotnet/2017/10/26/entity-framework-6-2-runtime-released/

Follow those steps:

  1. Copy the file migrate.exe from the '\packages\EntityFramework.6.2.0\tools' to the target 'bin' folder (for example on the production server) after that you deployed the new assembly that contains the new migrations
  2. Open the command line in the folder and launch this command:

migrate.exe yourMigrationAssembly.dll /startupConfigurationFile=”..\web.config” /scriptFile="migrationOutput.sql"

It will generate the the file "migrationOutput.sql" that contains the SQL you have to execute on your target environment DB based on the migrations that are not yet applied on it.

like image 171
Gianluigi Liguori Avatar answered Nov 11 '22 17:11

Gianluigi Liguori


It is currently not supported. Please add your vote to the issue: Migrations: -Script for Migrate.exe

like image 33
bricelam Avatar answered Nov 11 '22 17:11

bricelam