Skip to content

Commit 871c404

Browse files
committed
Update samples
1 parent 3c97663 commit 871c404

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

samples/Sample.Kafka.PostgreSql/Controllers/ValuesController.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Dapper;
55
using DotNetCore.CAP;
66
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.EntityFrameworkCore;
78
using Npgsql;
89

910
namespace 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)

samples/Sample.Kafka.PostgreSql/Sample.Kafka.PostgreSql.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
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" />

samples/Sample.Kafka.PostgreSql/Startup.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Storage;
24
using Microsoft.Extensions.DependencyInjection;
35

46
namespace 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

0 commit comments

Comments
 (0)