EF Migrations migrate.exe generate script

匿名 (未验证) 提交于 2019-12-03 08:41:19

问题:

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.

回答1:

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



回答2:

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;



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


回答4:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!