Utopia-Canteen-System/Data/DbContextFactory.cs

23 lines
769 B
C#

using Microsoft.EntityFrameworkCore;
using System.IO;
namespace UtopiaCanteenSystem.Data;
/// <summary>
/// Factory for creating AppDbContext instances. Used by RfidService and SyncService
/// to avoid sharing a single context across async operations.
/// </summary>
public class DbContextFactory : IDbContextFactory<AppDbContext>
{
private static readonly DbContextOptions<AppDbContext> Options;
static DbContextFactory()
{
var builder = new DbContextOptionsBuilder<AppDbContext>();
var dbPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "utopia_canteen.db");
builder.UseSqlite($"Data Source={dbPath}");
Options = builder.Options;
}
public AppDbContext CreateDbContext() => new AppDbContext(Options);
}