DB.cs 3.5 KB

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