using Microsoft.EntityFrameworkCore; public sealed class LabDb : DbContext { public string TenantId { get; } public LabDb(DbContextOptions options, string tenantId) : base(options) { if (string.IsNullOrWhiteSpace(tenantId)) throw new System.ArgumentException("Trusted tenant required"); TenantId = tenantId; } public DbSet Items => Set(); public DbSet Reservations => Set(); protected override void OnModelCreating(ModelBuilder model) { model.Entity().HasIndex(x => new { x.TenantId, x.Sku }).IsUnique(); model.Entity().Property(x => x.Version).IsConcurrencyToken(); model.Entity().HasQueryFilter("Tenant", x => x.TenantId == TenantId); model.Entity().HasQueryFilter("Tenant", x => x.TenantId == TenantId); model.Entity().HasIndex(x => new { x.TenantId, x.Reference }).IsUnique(); model.Entity().HasOne(x => x.Item).WithMany().HasForeignKey(x => x.ItemId).OnDelete(DeleteBehavior.Restrict); } } public sealed class Item { public int Id { get; set; } public string TenantId { get; set; } = ""; public string Sku { get; set; } = ""; public int Stock { get; set; } public int PriceCents { get; set; } public int Version { get; set; } } public sealed class Reservation { public int Id { get; set; } public string TenantId { get; set; } = ""; public string Reference { get; set; } = ""; public int ItemId { get; set; } public Item Item { get; set; } = null!; public int Quantity { get; set; } public long TotalCents { get; set; } }