25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

sqlserver.md 1.7 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # SQL Server
  2. SQL Server 是由微软开发的一个关系型数据库,你可以使用 SQL Server 来作为 CAP 消息的持久化。
  3. ## 配置
  4. 要使用 SQL Server 存储,你需要从 NuGet 安装以下扩展包:
  5. ```shell
  6. Install-Package DotNetCore.CAP.SqlServer
  7. ```
  8. 然后,你可以在 `Startup.cs` 的 `ConfigureServices` 方法中添加基于内存的配置项。
  9. ```csharp
  10. public void ConfigureServices(IServiceCollection services)
  11. {
  12. // ...
  13. services.AddCap(x =>
  14. {
  15. x.UsePostgreSql(opt=>{
  16. //SqlServerOptions
  17. });
  18. // x.UseXXX ...
  19. });
  20. }
  21. ```
  22. #### SqlServerOptions
  23. NAME | DESCRIPTION | TYPE | DEFAULT
  24. :---|:---|---|:---
  25. Schema | 数据库架构 | string | Cap
  26. ConnectionString | 数据库连接字符串 | string |
  27. ## Publish with transaction
  28. ### ADO.NET with transaction
  29. ```csharp
  30. private readonly ICapPublisher _capBus;
  31. using (var connection = new SqlConnection("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. ```