Simple migration for databases on .NET
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddDbMigrator(cfg => cfg.Use<NpgsqlMigrationContext>( ... ));
}
Usage NpgsqlMigrationContext
allow you migrate postgresql database. NpgsqlMigrationContext
is available in repo DbMigrator.Npgsql. If you want use other provider, you can implement own migration context by inherit IMigrationContext.
public void Configure(IApplicationBuilder app)
{
// ...
app.UseDbMigrator(async migrator => await migrator.Migrate());
// or
app.UseDbMigrator(async migrator => await migrator.Downgrade(1));
}
Now create your migration which inherits from MigrationBase
abstract class.
public class CreateTableMigration : MigrationBase
{
public override long Key => 1;
public override void Up(IMigrationAction action)
{
action.ExecuteSql("CREATE TABLE test(id int, name text);");
}
public override void Down(IMigrationAction action)
{
action.ExecuteSql("DROP TABLE test;");
}
}
All implementations of IMigration
will automatically added to service collection, if they are placed in current assembly. If not, then you can pointed which assembly to use, as shown below
services.AddDbMigrator(cfg => cfg.Use<NpgsqlMigrationContext>( ... ), typeof(CreateTableMigration).Assembly);
All contributions are welcome. I will glad to discuss all suggestions and troubles.