Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 226 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Automate EF-Migrations "Update-Database -Script"

#1
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 DBA.

I was able to generate SQL scripts with ``Update-Database -Script ... ``

However, I'd like to automate this. I hoped that the -Script switch would accept a parameter that specifies where to write the SQL to, but this is not the case. Also, output redirection does not work, because the SQL script is not written to STDOUT, but to a temporary file. I see no way of getting hold of that temp file from a script.

Any ideas how to automate the Migrations -> SQL Script generation? Maybe there is some magic powershell trickery that I'm not aware of?

EDIT: By the way, using migrate.exe or any other "migration-aware" approach is not an option, delivering SQL scripts is a must.
Reply

#2
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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through