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.

mysql.md 1.6 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # MySQL
  2. MySQL is an open-source relational database management system. CAP supports MySQL database.
  3. ## Configuration
  4. To use MySQL storage, you need to install the following package from NuGet:
  5. ```powershell
  6. PM> Install-Package DotNetCore.CAP.MySql
  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.UseMySql(opt=>{
  16. //MySqlOptions
  17. });
  18. // x.UseXXX ...
  19. });
  20. }
  21. ```
  22. #### MySqlOptions
  23. NAME | DESCRIPTION | TYPE | DEFAULT
  24. :---|:---|---|:---
  25. TableNamePrefix | CAP table name prefix | string | cap
  26. ConnectionString | Database connection string | string | null
  27. ## Publish with transaction
  28. ### ADO.NET with transaction
  29. ```csharp
  30. private readonly ICapPublisher _capBus;
  31. using (var connection = new MySqlConnection(AppDbContext.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. ```