public async Task <int> SampleMethodAsync(int baseNum) { int i; const string identifier = @"X000B"; using (var txScope = await _dbManager.BeginTranRequiredAsync()) { i = await txScope.DbConnection.ExecuteAsync( "INSERT INTO [SampleTable01] VALUES (@SampleColumn01)", new { SampleColumn01 = identifier }, txScope.DbTransaction); // i == 1 i += txScope.DbContext.Set <SampleTable01>() .AsNoTracking() .Count(e => e.SampleColumn01 == identifier); // i == 2 txScope.VoteCommit(); } _logger.LogInformation($"{i}"); return(baseNum + i); }
public async Task <int> SampleMethodAsync() { int i; using (var txScope = await _dbManager.BeginTranRequiredAsync()) { const string identifier = @"X000A"; // Dapper i = (await txScope.DbConnection.QueryAsync <int>( "SELECT COUNT(*) FROM [SampleTable01] WHERE [SampleColumn01] = @Value ", new { Value = identifier }, txScope.DbTransaction)) .FirstOrDefault(); // i == 0 // EF Core i += txScope.DbContext.Set <SampleTable01>() .AsNoTracking() .Count(e => e.SampleColumn01 == identifier); // i == 0 // add By EF Core with SaveChangesAsync() await txScope.DbContext.Set <SampleTable01>().AddAsync(new SampleTable01 { SampleColumn01 = identifier }); await txScope.DbContext.SaveChangesAsync(); i += txScope.DbContext.Set <SampleTable01>() .FromSqlInterpolated($"SELECT * FROM [SampleTable01] WHERE [SampleColumn01] = {identifier}") .Count(); // i == 1 i += (await txScope.DbConnection.QueryAsync <int>( "SELECT COUNT(*) FROM [SampleTable01] WHERE [SampleColumn01] = @Value ", new { Value = identifier }, txScope.DbTransaction)) .FirstOrDefault(); // i == 2 txScope.VoteCommit(); } _logger.LogInformation($"{i}"); return(i); }
public async Task <int> SampleMethodAsync() { int i; var identifierAll = new List <string> { "X000A", "X000B" }; using (var txScope = await _dbManager.BeginTranRequiredAsync()) { _logger.LogInformation($"txScope.Committable={txScope.Committable}"); // if you want to short-circuit evaluation. if (!txScope.Committable) { txScope.VoteRollback(); return(-1); } i = (await txScope.DbConnection.QueryAsync <int>( "SELECT COUNT(*) FROM [SampleTable01] WHERE [SampleColumn01] in @Values", new { Values = identifierAll }, txScope.DbTransaction)) .FirstOrDefault(); // i == 2 i += txScope.DbContext.Set <SampleTable01>() .AsNoTracking() .Count(e => identifierAll.Contains(e.SampleColumn01)); // i == 4 // txScope.VoteCommit(); // An explicit declaration is required. // Not specifying is the same as `.VoteRollback()`. } _logger.LogInformation($"{i}"); return(i); }
public async Task <IActionResult> Index() { var xA = await _sampleDiLogicA.SampleMethodAsync(); // xA = 2 var xB = await _sampleDiLogicB.SampleMethodAsync(xA); // xB = 4 var xC = await _sampleDiLogicC.SampleMethodAsync(); // xC = 0 var xD = await _sampleDiLogicD.SampleMethodAsync(); // xD = 4 // It's also possible to tx status check with in the Controller. using (var txScope = await _dbManager.BeginTranRequiredAsync()) { _logger.LogInformation($"txScope.Committable={txScope.Committable}"); // if(!txScope.Committable) // some tasks... txScope.VoteCommit(); // An explicit declaration is required. // Not specifying is the same as `.VoteRollback()`. } // In this sample, it is rolled back at the end. return(View()); }