Greg Smith
Greg Smith

Reputation: 2469

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.

Upvotes: 8

Views: 7023

Answers (4)

Gianluigi Liguori
Gianluigi Liguori

Reputation: 371

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.

Upvotes: 7

pwhe23
pwhe23

Reputation: 1334

You can write a simple c# console application or use something like Linqpad to generate the script using the Entity Framework Infrastructure objects. You will just need to load the DLL with your DbMigrationsConfiguration class and instantiate it. Here is the code similar to what is working 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);

Upvotes: 3

GregVang
GregVang

Reputation: 31

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

Upvotes: 3

bricelam
bricelam

Reputation: 30425

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

Upvotes: 5

Related Questions