public async Task Counts_when_SaveChanges_is_called(bool async) { TotalSaveChanges = 0; for (var i = 1; i <= 3; i++) { using var context = new SomeDbContext(); context.Add(new Foo()); _ = async ? await context.SaveChangesAsync() : context.SaveChanges(); Assert.Equal(i, TotalSaveChanges); } }
public async Task TestTransaction() { await using var transaction = await dbContext.Database.BeginTransactionAsync(IsolationLevel.RepeatableRead, _cancel.Token); var entities = await dbContext.SomeEntities.ToListAsync(_cancel.Token); entities.ShouldBeEmpty(); dbContext.SomeEntities.Add(new SomeEntity { Id = 1, Name = "srthsrt" }); await dbContext.SaveChangesAsync(_cancel.Token); await transaction.CommitAsync(_cancel.Token); }
public async Task Counts_when_DbUpdateConcurrencyException_is_thrown(bool async) { TotalOptimisticConcurrencyFailures = 0; for (var i = 1; i <= 3; i++) { using var context = new SomeDbContext(); var entity = new Foo(); context.Add(entity); context.SaveChanges(); using (var innerContext = new SomeDbContext()) { innerContext.Foos.Find(entity.Id).Token = 1; innerContext.SaveChanges(); } entity.Token = 2; if (async) { await Assert.ThrowsAsync <DbUpdateConcurrencyException>(async() => await context.SaveChangesAsync()); } else { Assert.Throws <DbUpdateConcurrencyException>(() => context.SaveChanges()); } Assert.Equal(i, TotalOptimisticConcurrencyFailures); } }