DB.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace DBHelper_SqlSugar
  6. {
  7. /// <summary>
  8. /// DB,禁止跨上下文使用
  9. /// 1.异步情况: 在同一串await 中是一个上下文 (await 会改变线程和同步是不一样的)
  10. /// 2.同步情况: 在同一个线程是同一个上下文
  11. /// </summary>
  12. public class Db
  13. {
  14. /// <summary>
  15. /// 上下文集合
  16. /// </summary>
  17. private static List<ContextList> _contexts = new List<ContextList>();
  18. /// <summary>
  19. /// 默认上下文类类型
  20. /// </summary>
  21. public static string DefaultDbContextType { get; private set; } = null!;
  22. public static T Do<T>(Func<Db, T> func)
  23. {
  24. var db = new Db();
  25. try
  26. {
  27. db.Default.BeginTran();
  28. var res = func(db);
  29. db.Default.CommitTran();
  30. return res;
  31. }
  32. catch (Exception ex)
  33. {
  34. //var qty = ex.EntityValidationErrors.Count();
  35. //var info = ex.EntityValidationErrors.First();
  36. //var msg = "有" + qty + "条数据验证失败,首条错误信息:\n" + string.Join("\n", info.MemberNames) + "\n" + (info.ErrorMessage ?? "");
  37. Console.WriteLine(ex.Message);
  38. throw new Exception(ex.Message);
  39. }
  40. }
  41. public static void Do(Action<Db> act)
  42. {
  43. Do(db =>
  44. {
  45. act(db);
  46. return 1;
  47. });
  48. }
  49. /// <summary>
  50. /// 设置默认链接
  51. /// </summary>
  52. /// <param name="key"></param>
  53. public static void SetDefaultDbContextType(string key)
  54. {
  55. DefaultDbContextType = key;
  56. }
  57. /// <summary>
  58. /// 默认链接
  59. /// </summary>
  60. public SqlSugarClient Default
  61. {
  62. get
  63. {
  64. if (DefaultDbContextType == null)
  65. throw new Exception("请先设置默认数据库,调用静态方法SetDefaultDbContextType()");
  66. return Context(DefaultDbContextType);
  67. }
  68. }
  69. /// <summary>
  70. /// 创建一个上下文
  71. /// </summary>
  72. /// <param name="type"></param>
  73. /// <returns></returns>
  74. public static SqlSugarClient CreateContext(ConnectionConfig config, string key)
  75. {
  76. var ctx = _contexts.FirstOrDefault(v => v.Key == key);
  77. if (ctx != null) return ctx.Client;
  78. ctx = new ContextList(key, new SqlSugarClient(new ConnectionConfig()
  79. {
  80. ConnectionString = config.ConnectionString,
  81. DbType = config.DbType,
  82. IsAutoCloseConnection = true
  83. }), config);
  84. _contexts.Add(ctx);
  85. return ctx.Client;
  86. }
  87. public SqlSugarClient Context(string key)
  88. {
  89. var ctx = _contexts.FirstOrDefault(v => v.Key == key);
  90. if (ctx == null) throw new Exception("没有对应的链接,请先调用创建");
  91. return ctx.Client;
  92. }
  93. }
  94. /// <summary>
  95. /// 链接
  96. /// </summary>
  97. public class ContextList
  98. {
  99. public ContextList(string key, SqlSugarClient client, ConnectionConfig connectionConfig)
  100. {
  101. this.Key = key;
  102. Client = client;
  103. ConnectionConfig = connectionConfig;
  104. }
  105. public string Key { get; set; }
  106. public SqlSugarClient Client { get; set; }
  107. public ConnectionConfig ConnectionConfig { get; set; }
  108. }
  109. }