public async Task CreatesInMemComposite() { var parent = new EfCoreTest { Id = 99999, Name = $"{nameof(BulkAddTests)}_Composite", CreatedDate = DateTime.UtcNow, ModifiedDate = DateTime.UtcNow }; using (var db = new woodmanContext(InMemDbOpts)) { await db.AddAsync(parent); await db.SaveChangesAsync(); } var toCreate = new List <EfCoreTestChild>(); for (var i = 0; i < 10; i++) { toCreate.Add(new EfCoreTestChild { Id = i + 1, EfCoreTestId = parent.Id, Name = i == 0 ? null : $"{nameof(BulkAddTests)}_{i}", CreatedDate = DateTime.UtcNow, ModifiedDate = DateTime.UtcNow }); } using (var db = new woodmanContext(InMemDbOpts)) { await db.EfCoreTestChild.BulkAddAsync(toCreate); foreach (var e in toCreate) { var added = await db.EfCoreTestChild.FindAsync(e.Id, e.EfCoreTestId); Assert.NotNull(added); Assert.Equal(e.Name, added.Name); } } }
public async Task SqlRollback() { using (var db = new woodmanContext()) using (var scope = db.Database.CurrentTransaction ?? db.Database.BeginTransaction()) { var toAdd = new EfCoreTest { Name = $"{nameof(BulkTransactionTests)}_Added", CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now }; await db.EfCoreTest.AddAsync(toAdd); await db.SaveChangesAsync(false); var ids = await db.EfCoreTest .Where(x => x.Name.Contains(nameof(BulkTransactionTests))) .Select(x => x.Id) .ToListAsync(); await db.EfCoreTest .Where(x => x.Name.Contains(nameof(BulkTransactionTests))) .BulkRemoveAsync(ids); scope.Rollback(); } using (var db = new woodmanContext()) { var count = await db.EfCoreTest .Where(x => x.Name.Contains(nameof(BulkTransactionTests))) .CountAsync(); Assert.Equal(InitialCount, count); } }
public async Task MergesSql(BulkMergeNotMatchedBehavior notMatchedBehavior) { List <EfCoreTest> entities; var dontDelete = new EfCoreTest { Name = "dont delete", CreatedDate = DateTime.UtcNow, ModifiedDate = DateTime.UtcNow }; using (var db = new woodmanContext()) { entities = await db.EfCoreTest .Where(e => e.Name.Contains(nameof(BulkMergeTests))) .ToListAsync(); var added = await db.AddAsync(dontDelete); await db.SaveChangesAsync(); dontDelete.Id = added.Entity.Id; } const int numUpdate = 5; var toMerge = entities .Take(numUpdate) .Select(x => { x.ModifiedDate = DateTime.UtcNow.AddDays(x.Id); return(x); }) .ToList(); var numUnmatched = entities.Count - toMerge.Count; var numAdd = 2; for (var i = 0; i < numAdd; i++) { toMerge.Add(new EfCoreTest { Name = $"{nameof(BulkMergeTests)}_insert_{i}", ModifiedDate = DateTime.Now, CreatedDate = DateTime.Now }); } var expectedRecordsAffected = notMatchedBehavior == BulkMergeNotMatchedBehavior.DoNothing ? numUpdate + numAdd : numUpdate + numUnmatched + numAdd; using (var db = new woodmanContext()) { var numRowsAffected = await db.EfCoreTest .Where(e => e.Name.Contains(nameof(BulkMergeTests))) .BulkMergeAsync(toMerge, notMatchedBehavior: notMatchedBehavior, whenNotMatched: () => new EfCoreTest { Name = $"{nameof(BulkMergeTests)}__Archived__" }); Assert.Equal(expectedRecordsAffected, numRowsAffected); var dbCount = await db.EfCoreTest .Where(e => e.Name.Contains(nameof(BulkMergeTests))) .CountAsync(); if (notMatchedBehavior == BulkMergeNotMatchedBehavior.Delete) { Assert.Equal(toMerge.Count, dbCount); } else { Assert.NotEqual(toMerge.Count, dbCount); } foreach (var m in toMerge) { var dbEntity = await db.EfCoreTest.FindAsync(m.Id); Assert.NotNull(dbEntity); Assert.Equal(m.Name, dbEntity.Name); Assert.Equal(m.ModifiedDate.ToString("G"), dbEntity.ModifiedDate.ToString("G")); Assert.Equal(m.CreatedDate.ToString("G"), dbEntity.CreatedDate.ToString("G")); } var dontDeleteEntity = await db.EfCoreTest.FindAsync(dontDelete.Id); Assert.NotNull(dontDeleteEntity); } }