|
| 1 | +using Microsoft.EntityFrameworkCore; |
| 2 | + |
| 3 | +namespace SqlServerSimulator; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Exercises the simulator's <c>IDENTITY</c> column support through EF Core. |
| 7 | +/// SaveChanges on an EF-managed identity entity emits |
| 8 | +/// <c>INSERT ... OUTPUT INSERTED.Id</c> (single row) or <c>MERGE INTO ... OUTPUT</c> |
| 9 | +/// (multi-row) — neither is parsed yet, so that path is pinned as a gap by |
| 10 | +/// <see cref="Insert_IdentityKey_AwaitsOutputClauseSupport"/>. The other |
| 11 | +/// tests exercise three EF Core paths that <i>don't</i> require |
| 12 | +/// <c>OUTPUT</c>: read-only LINQ, <c>SqlQueryRaw</c> against the identity |
| 13 | +/// scalars, and <c>SaveChanges</c> with <c>DatabaseGenerated.None</c> + <c>SET IDENTITY_INSERT ON</c>. |
| 14 | +/// </summary> |
| 15 | +[TestClass] |
| 16 | +public class EFCoreIdentity |
| 17 | +{ |
| 18 | + public TestContext TestContext { get; set; } = null!; |
| 19 | + |
| 20 | + [TestMethod] |
| 21 | + public void Insert_IdentityKey_AwaitsOutputClauseSupport() |
| 22 | + { |
| 23 | + using var context = new TestDbContext(TestDbContext.CreateWidgetsSimulation()); |
| 24 | + |
| 25 | + var widget = new Widget { Name = "First" }; |
| 26 | + _ = context.Widgets.Add(widget); |
| 27 | + |
| 28 | + var ex = Assert.Throws<DbUpdateException>(() => context.SaveChanges()); |
| 29 | + StringAssert.Contains(ex.InnerException?.Message ?? "", "OUTPUT"); |
| 30 | + } |
| 31 | + |
| 32 | + [TestMethod] |
| 33 | + public void Query_IdentityRows_ReturnsAutoGeneratedIds() |
| 34 | + { |
| 35 | + var simulation = TestDbContext.CreateWidgetsSimulation(); |
| 36 | + _ = simulation |
| 37 | + .CreateOpenConnection() |
| 38 | + .CreateCommand("insert into Widgets (Name) values ('A'),('B'),('C')") |
| 39 | + .ExecuteNonQuery(); |
| 40 | + |
| 41 | + using var context = new TestDbContext(simulation); |
| 42 | + var rows = context.Widgets.OrderBy(w => w.Id).ToArray(); |
| 43 | + |
| 44 | + CollectionAssert.AreEqual(new[] { 1, 2, 3 }, rows.Select(w => w.Id).ToArray()); |
| 45 | + CollectionAssert.AreEqual(new[] { "A", "B", "C" }, rows.Select(w => w.Name).ToArray()); |
| 46 | + } |
| 47 | + |
| 48 | + [TestMethod] |
| 49 | + public void Where_FiltersByAutoGeneratedId() |
| 50 | + { |
| 51 | + var simulation = TestDbContext.CreateWidgetsSimulation(); |
| 52 | + _ = simulation |
| 53 | + .CreateOpenConnection() |
| 54 | + .CreateCommand("insert into Widgets (Name) values ('A'),('B'),('C')") |
| 55 | + .ExecuteNonQuery(); |
| 56 | + |
| 57 | + using var context = new TestDbContext(simulation); |
| 58 | + var name = context.Widgets.Where(w => w.Id == 2).Select(w => w.Name).Single(); |
| 59 | + |
| 60 | + Assert.AreEqual("B", name); |
| 61 | + } |
| 62 | + |
| 63 | + [TestMethod] |
| 64 | + public void SqlQueryRaw_ScopeIdentity_ReturnsLastInsertedValue() |
| 65 | + { |
| 66 | + // SCOPE_IDENTITY/@@IDENTITY are simulation-wide here (not per-session) — |
| 67 | + // see CLAUDE.md's "session-scoped state" gap. That collapse means the |
| 68 | + // raw INSERT on a separate connection still updates the value EF reads. |
| 69 | + var simulation = TestDbContext.CreateWidgetsSimulation(); |
| 70 | + _ = simulation |
| 71 | + .CreateOpenConnection() |
| 72 | + .CreateCommand("insert into Widgets (Name) values ('First'),('Second')") |
| 73 | + .ExecuteNonQuery(); |
| 74 | + |
| 75 | + using var context = new TestDbContext(simulation); |
| 76 | + var sid = context.Database.SqlQueryRaw<decimal>("select SCOPE_IDENTITY() as Value").AsEnumerable().Single(); |
| 77 | + var atid = context.Database.SqlQueryRaw<decimal>("select @@IDENTITY as Value").AsEnumerable().Single(); |
| 78 | + |
| 79 | + Assert.AreEqual(2m, sid); |
| 80 | + Assert.AreEqual(2m, atid); |
| 81 | + } |
| 82 | + |
| 83 | + [TestMethod] |
| 84 | + public void SqlQueryRaw_IdentCurrent_ReturnsHighWaterMark() |
| 85 | + { |
| 86 | + var simulation = TestDbContext.CreateWidgetsSimulation(); |
| 87 | + _ = simulation |
| 88 | + .CreateOpenConnection() |
| 89 | + .CreateCommand("insert into Widgets (Name) values ('A'),('B'),('C')") |
| 90 | + .ExecuteNonQuery(); |
| 91 | + |
| 92 | + using var context = new TestDbContext(simulation); |
| 93 | + var ic = context.Database.SqlQueryRaw<decimal>("select IDENT_CURRENT('Widgets') as Value").AsEnumerable().Single(); |
| 94 | + |
| 95 | + Assert.AreEqual(3m, ic); |
| 96 | + } |
| 97 | + |
| 98 | + [TestMethod] |
| 99 | + public void SqlQueryRaw_ScopeIdentity_BeforeAnyInsertIsNull() |
| 100 | + { |
| 101 | + using var context = new TestDbContext(TestDbContext.CreateWidgetsSimulation()); |
| 102 | + var sid = context.Database.SqlQueryRaw<decimal?>("select SCOPE_IDENTITY() as Value").AsEnumerable().Single(); |
| 103 | + |
| 104 | + Assert.IsNull(sid); |
| 105 | + } |
| 106 | + |
| 107 | + [TestMethod] |
| 108 | + public void SaveChanges_WithIdentityInsertOn_PersistsExplicitId() |
| 109 | + { |
| 110 | + var simulation = TestDbContext.CreateStickersSimulation(); |
| 111 | + _ = simulation |
| 112 | + .CreateOpenConnection() |
| 113 | + .CreateCommand("set identity_insert Stickers on") |
| 114 | + .ExecuteNonQuery(); |
| 115 | + |
| 116 | + using var context = new TestDbContext(simulation); |
| 117 | + _ = context.Stickers.Add(new Sticker { Id = 100, Tag = "explicit" }); |
| 118 | + _ = context.SaveChanges(); |
| 119 | + |
| 120 | + Assert.AreEqual("explicit", context.Stickers.Where(s => s.Id == 100).Select(s => s.Tag).Single()); |
| 121 | + } |
| 122 | + |
| 123 | + [TestMethod] |
| 124 | + public void SaveChanges_WithIdentityInsertOn_AdvancesSeed() |
| 125 | + { |
| 126 | + // Inserting an explicit Id past the high-water mark via EF should |
| 127 | + // reseed the same way raw SQL does. |
| 128 | + var simulation = TestDbContext.CreateStickersSimulation(); |
| 129 | + _ = simulation |
| 130 | + .CreateOpenConnection() |
| 131 | + .CreateCommand("set identity_insert Stickers on") |
| 132 | + .ExecuteNonQuery(); |
| 133 | + |
| 134 | + using (var context = new TestDbContext(simulation)) |
| 135 | + { |
| 136 | + _ = context.Stickers.Add(new Sticker { Id = 50, Tag = "jump" }); |
| 137 | + _ = context.SaveChanges(); |
| 138 | + } |
| 139 | + |
| 140 | + var ic = simulation |
| 141 | + .CreateOpenConnection() |
| 142 | + .CreateCommand("select IDENT_CURRENT('Stickers')") |
| 143 | + .ExecuteScalar(); |
| 144 | + Assert.AreEqual(50m, ic); |
| 145 | + } |
| 146 | +} |
0 commit comments