-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampleDiLogicA.cs
73 lines (60 loc) · 2.37 KB
/
SampleDiLogicA.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using OneWorldDbClient.SampleWeb.Data;
namespace OneWorldDbClient.SampleWeb.SampleDi
{
public class SampleDiLogicA
{
private readonly OneWorldDbClientManager<ApplicationDbContext> _dbManager;
private readonly ILogger<SampleDiLogicA> _logger;
public SampleDiLogicA(
OneWorldDbClientManager<ApplicationDbContext> dbManager,
ILogger<SampleDiLogicA> logger)
{
_dbManager = dbManager;
_logger = logger;
}
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;
}
}
}