Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arichika/385ec269bdd229b73768f6a1ec47cb8e to your computer and use it in GitHub Desktop.
Save arichika/385ec269bdd229b73768f6a1ec47cb8e to your computer and use it in GitHub Desktop.
Blog:OneWorldDbClient:201908261200:SampleDiLogicA.cs
// SampleDiLogicA.cs
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment