using System; using System.Linq; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; await using var connection = new SqliteConnection("Data Source=:memory:;Foreign Keys=True"); await connection.OpenAsync(); var options = new DbContextOptionsBuilder().UseSqlite(connection).Options; await using (var seed = new LabDb(options, "A")) { await seed.Database.EnsureCreatedAsync(); // Trusted fixture seeding is intentionally cross-tenant; query filters do not enforce writes. seed.Items.AddRange(new Item { TenantId = "A", Name = "VisibleA" }, new Item { TenantId = "A", Name = "DeletedA", IsDeleted = true }, new Item { TenantId = "B", Name = "VisibleB" }, new Item { TenantId = "B", Name = "DeletedB", IsDeleted = true }); await seed.SaveChangesAsync(); } await using (var tenantA = new LabDb(options, "A")) { var visible = await tenantA.Items.AsNoTracking().SingleAsync(); Check(visible.Name == "VisibleA", "tenant A normal"); Console.WriteLine("tenant-a:" + visible.Name); var withDeleted = await tenantA.Items.IgnoreQueryFilters(["NotDeleted"]).AsNoTracking().ToListAsync(); Check(withDeleted.Count == 2 && withDeleted.All(x => x.TenantId == "A"), "selective filter keeps tenant"); Console.WriteLine("with-deleted:2"); Check(await tenantA.Items.IgnoreQueryFilters().CountAsync() == 4, "privileged all filters off"); } await using (var tenantB = new LabDb(options, "B")) { Check((await tenantB.Items.SingleAsync()).Name == "VisibleB", "context tenant parameter"); Console.WriteLine("tenant-b:1"); } Console.WriteLine("all-filters-off:4"); static void Check(bool condition, string name) { if (!condition) throw new InvalidOperationException("CHECK: " + name); }