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 44using Dapper ;
55using DotNetCore . CAP ;
66using Microsoft . AspNetCore . Mvc ;
7+ using Microsoft . EntityFrameworkCore ;
78using Npgsql ;
89
910namespace Sample . Kafka . PostgreSql . Controllers
@@ -24,7 +25,7 @@ public async Task<IActionResult> Stop([FromServices] IBootstrapper bootstrapper)
2425 await bootstrapper . DisposeAsync ( ) ;
2526 return Ok ( ) ;
2627 }
27-
28+
2829
2930 [ Route ( "~/delay/{delaySeconds:int}" ) ]
3031 public async Task < IActionResult > Delay ( int delaySeconds )
@@ -64,6 +65,22 @@ public IActionResult AdonetWithTransaction()
6465 return Ok ( ) ;
6566 }
6667
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+
6784
6885 [ CapSubscribe ( "sample.kafka.postgrsql" ) ]
6986 public void Test2 ( DateTime value )
Original file line number Diff line number Diff line change 88
99 <ItemGroup >
1010 <PackageReference Include =" Dapper" Version =" 2.0.138" />
11+ <PackageReference Include =" Npgsql.EntityFrameworkCore.PostgreSQL" Version =" 8.0.11" />
1112 </ItemGroup >
1213 <ItemGroup >
1314 <ProjectReference Include =" ..\..\src\DotNetCore.CAP.Dashboard\DotNetCore.CAP.Dashboard.csproj" />
Original file line number Diff line number Diff line change 11using Microsoft . AspNetCore . Builder ;
2+ using Microsoft . EntityFrameworkCore ;
3+ using Microsoft . EntityFrameworkCore . Storage ;
24using Microsoft . Extensions . DependencyInjection ;
35
46namespace Sample . Kafka . PostgreSql
@@ -9,8 +11,15 @@ public class Startup
911
1012 public void ConfigureServices ( IServiceCollection services )
1113 {
14+ services . AddDbContext < AppDbContext > ( ( sp , opt ) =>
15+ {
16+ opt . UseNpgsql ( DbConnectionString )
17+ . ReplaceService < IRelationalConnection , CapNpgsqlRelationalConnection > ( ) ;
18+ } ) ;
19+
1220 services . AddCap ( x =>
1321 {
22+ //x.UseEntityFramework<AppDbContext>();
1423 //docker run --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgres
1524 x . UsePostgreSql ( DbConnectionString ) ;
1625
You can’t perform that action at this time.
0 commit comments