Skip to content

Instantly share code, notes, and snippets.

@ssougnez
Created January 14, 2026 20:43
Show Gist options
  • Select an option

  • Save ssougnez/81a3f5bc2a44d2fbd8554752a5ea8ff1 to your computer and use it in GitHub Desktop.

Select an option

Save ssougnez/81a3f5bc2a44d2fbd8554752a5ea8ff1 to your computer and use it in GitHub Desktop.
public class AppMigrationEngine: BaseMigrationEngine
{
private readonly AppDbContext _db;
public AppMigrationEngine(AppDbContext db)
{
_db = db;
}
public override async Task <IEnumerable<Version>> GetAppliedVersionsAsync()
{
// Important: this method is called BEFORE EF Core migrations run.
// On a fresh database, your table won't exist yet.
if (!await _db.Database.CanConnectAsync()) return [];
try
{
return await _db.MigrationHistory.Select(m => m.Version).ToListAsync();
}
catch
{
return [];
}
}
public override async Task RegisterVersionAsync(Version version)
{
_db.MigrationHistory.Add(new MigrationRecord
{
Version = version,
AppliedAt = DateTime.UtcNow
});
await _db.SaveChangesAsync();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment