views:

320

answers:

3

Hello,

is there a way to run migrations from within the application itself?

Thanks!

+4  A: 

I instantiate an instance of the Migrator class, and then you can call member methods like MigrateToLastVersion() or MigrateTo(long versionnr)

Migrator.Migrator m = new Migrator.Migrator ("SqlServer", connectionString, migrationsAssembly)

m.MigrateToLastVersion();
Frederik Gheysels
Sorry, but I don't get it...I made a class-projekt and did everything like in the tutorials.Then i made a new web-project and included the migration.dll and my compiled assembly.Then I included your source-code, but where can I find the right migration-assembly? It only finds my namespace "DBMigration", but there comes an error message "DBMigration is a namespace, but its used as a type". Can you help me?
Lichtamberg
Solved it... With System.Reflection.Assembly.Load(...)
Lichtamberg
That's indeed how it should be done. :) Load the assembly file that contains your migrations into an Assembly instance, and pass it to the Migrator class. :)
Frederik Gheysels
Get your assembly like this: var asm = Assembly.GetAssembly(typeof(Migration_0001));
Lance Fisher
A: 

I don't see why not.

Have a look at the nant task http://code.google.com/p/migratordotnet/source/browse/trunk/src/Migrator.NAnt/MigrateTask.cs

Relevant bits are here:

    private void Execute(Assembly asm)
    {
        Migrator mig = new Migrator(Provider, ConnectionString, asm, Trace, new TaskLogger(this));
        mig.DryRun = DryRun;
        if (ScriptChanges)
        {
            using (StreamWriter writer = new StreamWriter(ScriptFile))
            {
                mig.Logger = new SqlScriptFileLogger(mig.Logger, writer);
                RunMigration(mig);
            }
        }
        else
        {
            RunMigration(mig);
        }
    }

    private void RunMigration(Migrator mig)
    {
        if (mig.DryRun)
            mig.Logger.Log("********** Dry run! Not actually applying changes. **********");

        if (_to == -1)
            mig.MigrateToLastVersion();
        else
            mig.MigrateTo(_to);
    }
Derek Ekins
A: 

Yes you can add a call in your Global.asax

protected void Application_Start()
{
    // whatever code you want
}

This code will then get called each time the website loads up.

Garry Shutler