EF Migrations migrate.exe will generate a script

I play with Entity infrastructure and continuous builds. So far, I can perform a migration or a series of migrations without any problems using migrate.exe and the corresponding arguments.

However, I ran into a problem while trying to get the migrate.exe file in order to release the script and not perform the migration, just as I could get by running

update-database -TargetMigration TestMigration -script 

from the package manager console in Visual Studio.

Is there any way to do this?

Thanks.

+8
migration entity-framework
source share
4 answers

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

+5
source share

I encountered the same problem, and indeed, this option is available in the console in Visual Studio . So I opened the powershell script and dll entity framework and built a small executable so that you can generate scripts from the command line . The source code is available as is and without any warranty;

+3
source share

You can write a simple C # console application or use something like Linqpad to generate a script using Entity Framework infrastructure objects. You just need to load the DLL using the DbMigrationsConfiguration class and instantiate it. Here is a code similar to what works for me:

 using System.Data.Entity.Infrastructure; using System.Data.Entity.Migrations; using System.Data.Entity.Migrations.Infrastructure; const string ScriptFile = "Migration.sql"; const string ConnectionString = @"Server=.\SqlExpress;Database=...;Trusted_Connection=True;"; const bool AutomaticMigrationDataLossAllowed = false; var targetDb = new DbConnectionInfo(ConnectionString, "System.Data.SqlClient"); var config = new MyDbMigrationsConfiguration { AutomaticMigrationDataLossAllowed = AutomaticMigrationDataLossAllowed, TargetDatabase = targetDb, }; var migrator = new DbMigrator(config); var scripter = new MigratorScriptingDecorator(migrator); var script = scripter.ScriptUpdate(null, null); File.WriteAllText(ScriptFile, script); Console.WriteLine("Migration Script Generated: " + ScriptFile); 
+2
source share

From 10.22.2017 you can do this thanks to this PR: https://github.com/aspnet/EntityFramework6/commit/02ec6b8c9279f93f80eeed1234e5ce0acfce5f31

In this release of Notes Entity Framework 6.2, which implements this functionality (see "Migrate.exe must support - script option" section): https://blogs.msdn.microsoft.com/dotnet/2017/10/26/entity- framework-6-2-runtime-released /

Follow these steps:

  • Copy the migrate.exe file from "\ packages \ EntityFramework.6.2.0 \ tools" to the target bin folder (for example, on the production server), after which you deployed a new assembly that contains new migrations
  • Open a command prompt in a folder and run this command:

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

It will generate a migrationOutput.sql file that contains the SQL that you must run in your target DB environment, based on migrations that are not yet applied to it.

+1
source share

All Articles