可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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:
- 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
- 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.