1+ using System . Data ;
12using EntityFrameworkCore . Ydb . Extensions ;
23using Internal ;
34using Microsoft . EntityFrameworkCore ;
4- using Microsoft . EntityFrameworkCore . Infrastructure ;
55using Ydb . Sdk ;
6+ using Ydb . Sdk . Ado ;
67
78namespace EF ;
89
9- public class SloTableContext : SloTableContext < PooledDbContextFactory < TableDbContext > >
10+ public class SloTableContext : SloTableContext < Func < TableDbContext > >
1011{
1112 protected override string Job => "EF" ;
1213
13- protected override PooledDbContextFactory < TableDbContext > CreateClient ( Config config ) =>
14- new ( new DbContextOptionsBuilder < TableDbContext > ( ) . UseYdb ( config . ConnectionString ) . Options ) ;
14+ protected override Func < TableDbContext > CreateClient ( Config config ) =>
15+ ( ) => new TableDbContext ( new DbContextOptionsBuilder < TableDbContext > ( ) . UseYdb ( config . ConnectionString )
16+ . UseLoggerFactory ( ISloContext . Factory ) . Options ) ;
1517
1618 protected override async Task Create (
17- PooledDbContextFactory < TableDbContext > client ,
19+ Func < TableDbContext > client ,
1820 int operationTimeout
1921 )
2022 {
21- await using var dbContext = await client . CreateDbContextAsync ( ) ;
23+ await using var dbContext = client ( ) ;
2224 await dbContext . Database . EnsureCreatedAsync ( ) ;
2325 await dbContext . Database . ExecuteSqlRawAsync ( SloTable . Options ) ;
2426 }
2527
2628 protected override async Task < ( int , StatusCode ) > Save (
27- PooledDbContextFactory < TableDbContext > client ,
29+ Func < TableDbContext > client ,
2830 SloTable sloTable ,
2931 int writeTimeout
3032 )
3133 {
32- await using var dbContext = await client . CreateDbContextAsync ( ) ;
33- dbContext . SloEntities . Add ( sloTable ) ;
34- await dbContext . SaveChangesAsync ( ) ;
34+ await using var dbContext = client ( ) ;
35+ var executeStrategy = dbContext . Database . CreateExecutionStrategy ( ) ;
36+ await executeStrategy . ExecuteAsync ( async ( ) => await dbContext . Database . ExecuteSqlRawAsync (
37+ $ "UPSERT INTO `{ SloTable . Name } ` (Guid, Id, PayloadStr, PayloadDouble, PayloadTimestamp) " +
38+ "VALUES (@Guid, @Id, @PayloadStr, @PayloadDouble, @PayloadTimestamp)" ,
39+ new YdbParameter
40+ {
41+ DbType = DbType . String ,
42+ ParameterName = "Guid" ,
43+ Value = sloTable . Guid . ToString ( )
44+ } ,
45+ new YdbParameter
46+ {
47+ DbType = DbType . Int32 ,
48+ ParameterName = "Id" ,
49+ Value = sloTable . Id
50+ } ,
51+ new YdbParameter
52+ {
53+ DbType = DbType . String ,
54+ ParameterName = "PayloadStr" ,
55+ Value = sloTable . PayloadStr
56+ } ,
57+ new YdbParameter
58+ {
59+ DbType = DbType . Double ,
60+ ParameterName = "PayloadDouble" ,
61+ Value = sloTable . PayloadDouble
62+ } ,
63+ new YdbParameter
64+ {
65+ DbType = DbType . DateTime2 ,
66+ ParameterName = "PayloadTimestamp" ,
67+ Value = sloTable . PayloadTimestamp
68+ } ) ) ;
3569
3670 return ( 1 , StatusCode . Success ) ;
3771 }
3872
3973 protected override async Task < ( int , StatusCode , object ? ) > Select (
40- PooledDbContextFactory < TableDbContext > client ,
74+ Func < TableDbContext > client ,
4175 ( Guid Guid , int Id ) select ,
4276 int readTimeout
4377 )
4478 {
45- await using var dbContext = await client . CreateDbContextAsync ( ) ;
46- await dbContext . SloEntities . FindAsync ( select . Guid , select . Id ) ;
79+ await using var dbContext = client ( ) ;
80+ await dbContext . SloEntities
81+ . SingleAsync ( table => table . Guid == select . Guid && table . Id == select . Id ) ;
4782
4883 return ( 0 , StatusCode . Success , null ) ;
4984 }
5085
51- protected override async Task < int > SelectCount ( PooledDbContextFactory < TableDbContext > client )
86+ protected override async Task < int > SelectCount ( Func < TableDbContext > client )
5287 {
53- await using var dbContext = await client . CreateDbContextAsync ( ) ;
88+ await using var dbContext = client ( ) ;
89+ var count = await dbContext . SloEntities . CountAsync ( ) ;
5490
55- return await dbContext . SloEntities . CountAsync ( ) ;
91+ return count ;
5692 }
5793}
0 commit comments