Automate EF-Migrations “Update-Database -Script”

前端 未结 1 1235
醉酒成梦
醉酒成梦 2021-01-30 11:38

I\'m using EF migrations to track changes to our EF code-first DB model. Now I need to generate one SQL-Script for each migration, so that I can pass that set of scripts to the

1条回答
  •  孤独总比滥情好
    2021-01-30 12:19

    Finally I found a solution. What I wasn't aware of is that it is possible to generate SQL scripts from C# code as follows:

    using System.Data.Entity.Migrations;
    using System.Data.Entity.Migrations.Infrastructure;
    
    var migrator = new DbMigrator(new Configuration());
    var scriptor = new MigratorScriptingDecorator(migrator);
    var sql = scriptor.ScriptUpdate("Name-Of-Source-Migration", "Name-Of-Target-Migration");
    

    Together with migrator.GetLocalMigrations() you have full control over the granularity of the generated scripts.

    0 讨论(0)
提交回复
热议问题