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 db = new LabDb(options); await db.Database.EnsureCreatedAsync(); db.Items.Add(new Item { Name = "rune" }); await db.SaveChangesAsync(); using var owner = new CancellationTokenSource(); owner.Cancel(); bool cancelled = false; try { await db.Items.AsNoTracking().ToListAsync(owner.Token); } catch (OperationCanceledException) { cancelled = true; } Check(cancelled, "pre-cancelled EF query"); await using var verify = new LabDb(options); Check(await verify.Items.CountAsync() == 1, "stored row unchanged"); Console.WriteLine("cancelled"); Console.WriteLine("rows:1"); static void Check(bool condition, string name) { if (!condition) throw new InvalidOperationException("CHECK: " + name); }