using Microsoft.EntityFrameworkCore; namespace MeetingAssistant.Speakers; public sealed class SpeakerIdentityDatabaseInitializer : IHostedService { private static readonly SemaphoreSlim InitializeLock = new(1, 1); private readonly IDbContextFactory dbContextFactory; public SpeakerIdentityDatabaseInitializer(IDbContextFactory dbContextFactory) { this.dbContextFactory = dbContextFactory; } public async Task StartAsync(CancellationToken cancellationToken) { await InitializeLock.WaitAsync(cancellationToken); try { await using var context = await dbContextFactory.CreateDbContextAsync(cancellationToken); await SpeakerIdentitySchema.EnsureCreatedOrUpdatedAsync(context, cancellationToken); } finally { InitializeLock.Release(); } } public Task StopAsync(CancellationToken cancellationToken) { return Task.CompletedTask; } }