24 lines
804 B
C#
24 lines
804 B
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using WorkTime.Models;
|
|
|
|
namespace WorkTime.Database;
|
|
|
|
internal sealed class DatabaseContext(DbContextOptions<DatabaseContext> options) : DbContext(options) {
|
|
|
|
public DbSet<TimeEntry> Entries { get; set; }
|
|
|
|
}
|
|
|
|
internal sealed class DbMigrationService(IServiceProvider provider) : IHostedService {
|
|
public async Task StartAsync(CancellationToken cancellationToken) {
|
|
await using var scope = provider.CreateAsyncScope();
|
|
var context = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
|
|
|
|
await context.Database.MigrateAsync(cancellationToken);
|
|
}
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
|
}
|