Last active
September 1, 2022 01:58
-
-
Save gistlyn/50df00df4b3b9faa94a73d32ab4b2484 to your computer and use it in GitHub Desktop.
Add OrmLite DB Migrations to an existing project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ "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