Skip to content

Commit e6472dd

Browse files
Update SloTableContext.cs
1 parent 7173b04 commit e6472dd

File tree

1 file changed

+15
-54
lines changed

1 file changed

+15
-54
lines changed

slo/src/EF/SloTableContext.cs

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,55 @@
1-
using System.Data;
21
using EntityFrameworkCore.Ydb.Extensions;
32
using Internal;
43
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.EntityFrameworkCore.Infrastructure;
55
using Ydb.Sdk;
6-
using Ydb.Sdk.Ado;
76

87
namespace EF;
98

10-
public class SloTableContext : SloTableContext<Func<TableDbContext>>
9+
public class SloTableContext : SloTableContext<PooledDbContextFactory<TableDbContext>>
1110
{
1211
protected override string Job => "EF";
1312

14-
protected override Func<TableDbContext> CreateClient(Config config) =>
15-
() => new TableDbContext(new DbContextOptionsBuilder<TableDbContext>().UseYdb(config.ConnectionString).Options);
13+
protected override PooledDbContextFactory<TableDbContext> CreateClient(Config config) =>
14+
new(new DbContextOptionsBuilder<TableDbContext>().UseYdb(config.ConnectionString).Options);
1615

1716
protected override async Task Create(
18-
Func<TableDbContext> client,
17+
PooledDbContextFactory<TableDbContext> client,
1918
int operationTimeout
2019
)
2120
{
22-
await using var dbContext = client();
21+
await using var dbContext = await client.CreateDbContextAsync();
2322
await dbContext.Database.EnsureCreatedAsync();
2423
await dbContext.Database.ExecuteSqlRawAsync(SloTable.Options);
2524
}
2625

2726
protected override async Task<(int, StatusCode)> Save(
28-
Func<TableDbContext> client,
27+
PooledDbContextFactory<TableDbContext> client,
2928
SloTable sloTable,
3029
int writeTimeout
3130
)
3231
{
33-
await using var context = client();
34-
var executeStrategy = context.Database.CreateExecutionStrategy();
35-
await executeStrategy.ExecuteAsync(async () =>
36-
{
37-
await using var dbContext = client();
38-
return await dbContext.Database.ExecuteSqlRawAsync(
39-
$"UPSERT INTO `{SloTable.Name}` (Guid, Id, PayloadStr, PayloadDouble, PayloadTimestamp) " +
40-
"VALUES (@Guid, @Id, @PayloadStr, @PayloadDouble, @PayloadTimestamp)",
41-
new YdbParameter
42-
{
43-
DbType = DbType.Guid,
44-
ParameterName = "Guid",
45-
Value = sloTable.Guid
46-
},
47-
new YdbParameter
48-
{
49-
DbType = DbType.Int32,
50-
ParameterName = "Id",
51-
Value = sloTable.Id
52-
},
53-
new YdbParameter
54-
{
55-
DbType = DbType.String,
56-
ParameterName = "PayloadStr",
57-
Value = sloTable.PayloadStr
58-
},
59-
new YdbParameter
60-
{
61-
DbType = DbType.Double,
62-
ParameterName = "PayloadDouble",
63-
Value = sloTable.PayloadDouble
64-
},
65-
new YdbParameter
66-
{
67-
DbType = DbType.DateTime2,
68-
ParameterName = "PayloadTimestamp",
69-
Value = sloTable.PayloadTimestamp
70-
});
71-
});
32+
await using var context = await client.CreateDbContextAsync();
33+
context.SloEntities.Add(sloTable);
34+
await context.SaveChangesAsync();
7235

7336
return (1, StatusCode.Success);
7437
}
7538

7639
protected override async Task<(int, StatusCode, object?)> Select(
77-
Func<TableDbContext> client,
40+
PooledDbContextFactory<TableDbContext> client,
7841
(Guid Guid, int Id) select,
7942
int readTimeout
8043
)
8144
{
82-
await using var dbContext = client();
45+
await using var dbContext = await client.CreateDbContextAsync();
8346
return (0, StatusCode.Success, await dbContext.SloEntities.FirstOrDefaultAsync(
8447
table => table.Guid == select.Guid && table.Id == select.Id));
8548
}
8649

87-
protected override async Task<int> SelectCount(Func<TableDbContext> client)
50+
protected override async Task<int> SelectCount(PooledDbContextFactory<TableDbContext> client)
8851
{
89-
await using var dbContext = client();
90-
var count = await dbContext.SloEntities.CountAsync();
91-
92-
return count;
52+
await using var dbContext = await client.CreateDbContextAsync();
53+
return await dbContext.SloEntities.CountAsync();
9354
}
9455
}

0 commit comments

Comments
 (0)