8

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.

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Migrate.exe also supports the new parameters sourceMigration and force.
5

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

1 Comment

Vote added. Thanks for coming back.
3

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;

1 Comment

try expanding on your answer a little bit. Instead of just posting a link, provide an explanation of what it entails or how it will help.
3

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.