-
Notifications
You must be signed in to change notification settings - Fork 34
Quickstart
There are two ways of using Mig#. You can use it as a full-blown light-weight database migration library and/or – if you prefer writing DDL statements in C# – just as a schema modification library. This Quickstart covers both modes.
In order to change the schema of a database, you simply create a instance of DbSchema
and call the Alter
method on it. Using the fluent interface of Mig#, altering the schema becomes easy and readable. For example:
var schema = new DbSchema(ConnectionString, DbName);
schema.Alter(db => db.Tables["Customer"].Rename("Customers"));
renames the “Customer” table to “Customers”.
First, you want to write a migration. You do this by implementing the IMigration
interface and applying the MigrationExportAttribute
. For example:
[MigrationExport]
internal class Migration1 : IMigration
{
public void Up(IDatabase db)
{
db.CreateTable(TableName)
.WithPrimaryKeyColumn(ColumnNames[0], DbType.Int32);
}
}
Note that the timestamp of the migration is inferred by its post-fix. In the example above, this is 1.
The actual execution of migrations on a specific database is performed using the Migrator
type. In order to instantiate it, you need to provide the connection string and the name of the database provider:
var migrator = new Migrator(connectionString, ProviderNames.SqlServer);
To update the database to the latest version, simply call MigrateAll
and pass-in the assembly where you have defined the migration(s). For example:
migrator.MigrateAll(typeof(Migration1).Assembly);
This was a super-fast quick-intro for the caffeine drinkers amongst you. For more details, see the Manual.