Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active September 1, 2022 01:58
Show Gist options
  • Save gistlyn/50df00df4b3b9faa94a73d32ab4b2484 to your computer and use it in GitHub Desktop.
Save gistlyn/50df00df4b3b9faa94a73d32ab4b2484 to your computer and use it in GitHub Desktop.
Add OrmLite DB Migrations to an existing project
using MyApp.Migrations;
using ServiceStack;
using ServiceStack.Data;
using ServiceStack.OrmLite;
[assembly: HostingStartup(typeof(MyApp.ConfigureDbMigrations))]
namespace MyApp;
// Code-First DB Migrations: https://docs.servicestack.net/ormlite/db-migrations
public class ConfigureDbMigrations : IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureAppHost(afterAppHostInit:appHost => {
var migrator = new Migrator(appHost.Resolve<IDbConnectionFactory>(), typeof(Migration1000).Assembly);
AppTasks.Register("migrate", _ => migrator.Run());
AppTasks.Register("migrate.revert", args => migrator.Revert(args[0]));
AppTasks.Run();
});
}
using ServiceStack.DataAnnotations;
using ServiceStack.OrmLite;
namespace MyApp.Migrations;
public class Migration1000 : MigrationBase
{
public class MyTable
{
[AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
}
public override void Up()
{
Db.CreateTable<MyTable>();
}
public override void Down()
{
Db.DropTable<MyTable>();
}
}
[
{ "op":"add", "path":"/scripts/postinstall", "value":"dotnet run --AppTasks=migrate" },
{ "op":"add", "path":"/scripts/migrate", "value":"dotnet run --AppTasks=migrate" },
{ "op":"add", "path":"/scripts/revert:last", "value":"dotnet run --AppTasks=migrate.revert:last" },
{ "op":"add", "path":"/scripts/revert:all", "value":"dotnet run --AppTasks=migrate.revert:all" },
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment