You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

postgresql.md 1.7 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Postgre SQL
  2. PostgreSQL is an open-source relational database management system. CAP has supported PostgreSQL as persistent.
  3. ## Configuration
  4. To use PostgreSQL storage, you need to install the following extensions from NuGet:
  5. ```powershell
  6. PM> Install-Package DotNetCore.CAP.PostgreSql
  7. ```
  8. Next, add configuration items to the `ConfigureServices` method of `Startup.cs`.
  9. ```csharp
  10. public void ConfigureServices(IServiceCollection services)
  11. {
  12. // ...
  13. services.AddCap(x =>
  14. {
  15. x.UsePostgreSql(opt=>{
  16. //PostgreSqlOptions
  17. });
  18. // x.UseXXX ...
  19. });
  20. }
  21. ```
  22. #### PostgreSqlOptions
  23. NAME | DESCRIPTION | TYPE | DEFAULT
  24. :---|:---|---|:---
  25. Schema | Database schema | string | cap
  26. ConnectionString | Database connection string | string |
  27. ## Publish with transaction
  28. ### ADO.NET with transaction
  29. ```csharp
  30. private readonly ICapPublisher _capBus;
  31. using (var connection = new NpgsqlConnection("ConnectionString"))
  32. {
  33. using (var transaction = connection.BeginTransaction(_capBus, autoCommit: false))
  34. {
  35. //your business code
  36. connection.Execute("insert into test(name) values('test')",
  37. transaction: (IDbTransaction)transaction.DbTransaction);
  38. _capBus.Publish("sample.rabbitmq.mysql", DateTime.Now);
  39. transaction.Commit();
  40. }
  41. }
  42. ```
  43. ### EntityFramework with transaction
  44. ```csharp
  45. private readonly ICapPublisher _capBus;
  46. using (var trans = dbContext.Database.BeginTransaction(_capBus, autoCommit: false))
  47. {
  48. dbContext.Persons.Add(new Person() { Name = "ef.transaction" });
  49. _capBus.Publish("sample.rabbitmq.mysql", DateTime.Now);
  50. dbContext.SaveChanges();
  51. trans.Commit();
  52. }
  53. ```