public void TransactionTest() { IServiceScope scope1 = null; try { _semaphore.Wait(); scope1 = Services.CreateScope(); var repository = scope1.ServiceProvider.GetRequiredService <IEFRepository <TestDbContext, TestEntity> >(); _output.WriteLine($"----- TransactionTest Begin {DateTime.UtcNow.Ticks} -----"); repository.DbContext.Database.EnsureCreated(); repository.Insert(new TestEntity() { CreatedAt = DateTime.UtcNow, Name = "xss", }); repository.Update(new TestEntity() { Id = 1, Name = new string('x', 6) }, "Name"); using (var scope = Services.CreateScope()) { var repo = scope.ServiceProvider.GetRequiredService <IEFRepository <TestDbContext, TestEntity> >(); repo.Insert(new TestEntity() { CreatedAt = DateTime.UtcNow, Name = new string('y', 6) }); } var beforeCount = repository.Count(); using var uow = repository.GetUnitOfWork(); uow.DbContext.Update(new TestEntity() { Id = 1, Name = new string('1', 6) }, "Name"); uow.DbContext.UpdateWithout(new TestEntity() { Id = 2, Name = new string('2', 6) }, x => x.CreatedAt); var entity = new TestEntity() { CreatedAt = DateTime.UtcNow, Name = "xyy1", }; uow.DbSet <TestEntity>().Add(entity); uow.DbSet <TestEntity>().Remove(entity); uow.DbSet <TestEntity>().Add(new TestEntity() { CreatedAt = DateTime.UtcNow, Name = "xyy1", }); var beforeCommitCount = repository.Count(); Assert.Equal(beforeCount, beforeCommitCount); uow.Commit(); var committedCount = repository.Count(); Assert.Equal(committedCount, beforeCount + 1); using (var scope = Services.CreateScope()) { var repo = scope.ServiceProvider.GetRequiredService <IEFRepositoryFactory <TestDbContext> >() .GetRepository <TestEntity>(); entity = repo.Find(1); Assert.Equal(new string('1', 6), entity.Name); entity = repo.Find(2); Assert.Equal(new string('2', 6), entity.Name); Assert.Equal(1, repo.Delete(1)); } } finally { Repository.DbContext.CleanData(); scope1?.Dispose(); _output.WriteLine($"----- TransactionTest End {DateTime.UtcNow.Ticks} -----"); _semaphore.Release(); } }
public async Task TransactionAsyncTest() { IServiceScope scope1 = null; try { await _semaphore.WaitAsync(); scope1 = Services.CreateScope(); var repository = scope1.ServiceProvider.GetRequiredService <IEFRepositoryFactory <TestDbContext> >() .GetRepository <TestEntity>(); _output.WriteLine($"----- TransactionAsyncTest Begin {DateTime.UtcNow.Ticks}-----"); repository.DbContext.Database.EnsureCreated(); //for (var i = 0; i < 3; i++) //{ // await Repository.InsertAsync(new TestEntity() // { // CreatedAt = DateTime.UtcNow, // Name = $"xss-{i}", // }); //} await repository.InsertAsync(new[] { new TestEntity() { CreatedAt = DateTime.UtcNow, Name = "xss1", }, new TestEntity() { CreatedAt = DateTime.UtcNow, Name = "xss2", }, new TestEntity() { CreatedAt = DateTime.UtcNow, Name = "xss3", } }); using (var scope = Services.CreateScope()) { var repo = scope.ServiceProvider.GetRequiredService <IEFRepository <TestDbContext, TestEntity> >(); await repo.InsertAsync(new TestEntity() { CreatedAt = DateTime.UtcNow, Name = "xxxxxx" }); } var beforeCount = await repository.CountAsync(); using var uow = repository.GetUnitOfWork(); uow.DbContext.Update(new TestEntity() { Id = 3, Name = new string('3', 6) }, "Name"); uow.DbContext.UpdateWithout(new TestEntity() { Id = 4, Name = new string('4', 6) }, x => x.CreatedAt); var entity = new TestEntity() { CreatedAt = DateTime.UtcNow, Name = "xyy1", }; uow.DbSet <TestEntity>().Add(entity); uow.DbSet <TestEntity>().Remove(entity); uow.DbSet <TestEntity>().Add(new TestEntity() { CreatedAt = DateTime.UtcNow, Name = "xyy1", }); var beforeCommitCount = await repository.CountAsync(); Assert.Equal(beforeCount, beforeCommitCount); await uow.CommitAsync(); var committedCount = await repository.CountAsync(); Assert.Equal(committedCount, beforeCount + 1); entity = await repository.DbContext.FindAsync <TestEntity>(3); Assert.Equal(new string('3', 6), entity.Name); entity = await repository.DbContext.FindAsync <TestEntity>(new object[] { 4 }, CancellationToken.None); Assert.Equal(new string('4', 6), entity.Name); Assert.Equal(1, await Repository.DeleteAsync(1)); } finally { Repository.DbContext.CleanData(); scope1?.Dispose(); _output.WriteLine($"----- TransactionAsyncTest End {DateTime.UtcNow.Ticks} -----"); _semaphore.Release(); } }