Created
August 26, 2019 03:20
-
-
Save arichika/385ec269bdd229b73768f6a1ec47cb8e to your computer and use it in GitHub Desktop.
Blog:OneWorldDbClient:201908261200:SampleDiLogicA.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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