public async Task Should_Create_Read_Update() { var team = new Team { Name = "Craig" }; await using (var context = new SportsContext(_options)) { await using var transaction = context.Database.BeginTransaction(); context.Add(team); await context.SaveChangesAsync(); await transaction.CommitAsync(); } await using (var context = new SportsContext(_options)) { var fetchTeam = (await new QueryTeam.ById(team.Id) .ExecuteAsync(context)).Single(); fetchTeam.Name = "Matthew"; await context.SaveChangesAsync(); await using var transaction = context.Database.BeginTransaction(); await context.SaveChangesAsync(); await transaction.CommitAsync(); } await using (var context = new SportsContext(_options)) { var fetchTeam = (await new QueryTeam.ById(team.Id) .ExecuteAsync(context)).Single(); Assert.AreEqual("Matthew", fetchTeam.Name); } await using (var context = new SportsContext(_options)) { await using var transaction = context.Database.BeginTransaction(); var fetchTeam = (await new QueryTeam.ById(team.Id) .ExecuteAsync(context)).Single(); fetchTeam.Name = "John"; await context.SaveChangesAsync(); await using (var context2 = new SportsContext(_options)) { await using var transaction2 = context2.Database.BeginTransaction( IsolationLevel.ReadCommitted); var fetchTeam2 = (await new QueryTeam.ById(team.Id) .ExecuteAsync(context2)).Single(); Assert.AreEqual("Matthew", fetchTeam2.Name); await transaction2.CommitAsync(); } await transaction.CommitAsync(); } }
public void TestInitialize() { var builder = new DbContextOptionsBuilder <SportsContext>() .UseSqlServer( $"Server=(LocalDB)\\MSSQLLocalDB;Database=sports_db_{Guid.NewGuid()};Trusted_Connection=True;MultipleActiveResultSets=true"); _options = builder.Options; _context = new SportsContext(_options); _context.Database.EnsureCreated(); Context = new ServiceCollection() .AddSingleton(_options) .AddMiruken(configure => { configure .PublicSources(sources => sources.FromAssemblyOf <UnitOfWorkTests>()) .WithEntityFrameworkCore(); }).Build(); }