I'm trying to create a console application for generating a SQL Migration script for automation purposes - the same script generated by:
Update-Database -Script
From Package Manager Console.
In the console app I've created I can generate the script if I reference the DLL directly that has my DBMigrationsConfiguration in it.
For example this works:
using System.Data.Entity.Migrations;
using System.Data.Entity.Migrations.Infrastructure;
using System.IO;
using TAPS.Infrastructure.Migrations;
namespace msg
{
class Program
{
static void Main(string[] args)
{
var migrator = new DbMigrator(new Configuration());
var scriptor = new MigratorScriptingDecorator(migrator);
var sql = scriptor.ScriptUpdate(null, null);
File.WriteAllText(@"c:\script.sql", sql);
}
}
}
Note that I have a direct reference to the DLL, a using statement and I use a new statement to instantiate the Config object.
Now if I try to do it via reflection I get back null from the line:
DbMigrationsConfiguration configuration = (DbMigrationsConfiguration)assembly.CreateInstance("TAPS.Infrastructure.Migrations, Configuration");
The full code sample follows that I'm trying to get working via reflection:
using System;
using System.Data.Entity.Migrations;
using System.Data.Entity.Migrations.Infrastructure;
using System.IO;
using System.Reflection;
namespace msg
{
class Program
{
static void Main(string[] args)
{
Assembly assembly = AppDomain.CurrentDomain.Load(File.ReadAllBytes(@"C:\DLLPath\TAPS.Infrastructure.dll"));
DbMigrationsConfiguration configuration = (DbMigrationsConfiguration)assembly.CreateInstance("TAPS.Infrastructure.Migrations, Configuration");
var migrator = new DbMigrator(configuration);
var scriptor = new MigratorScriptingDecorator(migrator);
var sql = scriptor.ScriptUpdate(null, null);
File.WriteAllText(@"c:\script.sql", sql);
}
}
}
The configuration variable comes back as null.