File tree Expand file tree Collapse file tree 3 files changed +28
-1
lines changed
samples/Sample.Kafka.PostgreSql Expand file tree Collapse file tree 3 files changed +28
-1
lines changed Original file line number Diff line number Diff line change 4
4
using Dapper ;
5
5
using DotNetCore . CAP ;
6
6
using Microsoft . AspNetCore . Mvc ;
7
+ using Microsoft . EntityFrameworkCore ;
7
8
using Npgsql ;
8
9
9
10
namespace Sample . Kafka . PostgreSql . Controllers
@@ -24,7 +25,7 @@ public async Task<IActionResult> Stop([FromServices] IBootstrapper bootstrapper)
24
25
await bootstrapper . DisposeAsync ( ) ;
25
26
return Ok ( ) ;
26
27
}
27
-
28
+
28
29
29
30
[ Route ( "~/delay/{delaySeconds:int}" ) ]
30
31
public async Task < IActionResult > Delay ( int delaySeconds )
@@ -64,6 +65,22 @@ public IActionResult AdonetWithTransaction()
64
65
return Ok ( ) ;
65
66
}
66
67
68
+ [ Route ( "~/ef/transaction" ) ]
69
+ public IActionResult EntityFrameworkWithTransaction ( [ FromServices ] AppDbContext dbContext )
70
+ {
71
+ using ( dbContext . Database . BeginTransaction ( producer , false ) )
72
+ {
73
+ dbContext . Persons . Add ( new Person ( ) { Name = "ef.transaction" , Age = 11 } ) ;
74
+
75
+ dbContext . SaveChanges ( ) ;
76
+
77
+ producer . Publish ( "sample.kafka.postgrsql" , DateTime . UtcNow ) ;
78
+
79
+ dbContext . Database . CommitTransaction ( ) ;
80
+ }
81
+ return Ok ( ) ;
82
+ }
83
+
67
84
68
85
[ CapSubscribe ( "sample.kafka.postgrsql" ) ]
69
86
public void Test2 ( DateTime value )
Original file line number Diff line number Diff line change 8
8
9
9
<ItemGroup >
10
10
<PackageReference Include =" Dapper" Version =" 2.0.138" />
11
+ <PackageReference Include =" Npgsql.EntityFrameworkCore.PostgreSQL" Version =" 8.0.11" />
11
12
</ItemGroup >
12
13
<ItemGroup >
13
14
<ProjectReference Include =" ..\..\src\DotNetCore.CAP.Dashboard\DotNetCore.CAP.Dashboard.csproj" />
Original file line number Diff line number Diff line change 1
1
using Microsoft . AspNetCore . Builder ;
2
+ using Microsoft . EntityFrameworkCore ;
3
+ using Microsoft . EntityFrameworkCore . Storage ;
2
4
using Microsoft . Extensions . DependencyInjection ;
3
5
4
6
namespace Sample . Kafka . PostgreSql
@@ -9,8 +11,15 @@ public class Startup
9
11
10
12
public void ConfigureServices ( IServiceCollection services )
11
13
{
14
+ services . AddDbContext < AppDbContext > ( ( sp , opt ) =>
15
+ {
16
+ opt . UseNpgsql ( DbConnectionString )
17
+ . ReplaceService < IRelationalConnection , CapNpgsqlRelationalConnection > ( ) ;
18
+ } ) ;
19
+
12
20
services . AddCap ( x =>
13
21
{
22
+ //x.UseEntityFramework<AppDbContext>();
14
23
//docker run --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgres
15
24
x . UsePostgreSql ( DbConnectionString ) ;
16
25
You can’t perform that action at this time.
0 commit comments