DbContext.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using SqlSugar;
  2. using System;
  3. namespace WCS.Core.DbHelper
  4. {
  5. public class DbContext
  6. {
  7. private static ConnectionConfig? _config = null;
  8. public static void SetConfig(ConnectionConfig value)
  9. {
  10. _config = value;
  11. }
  12. /// <summary>
  13. /// 用单例模式
  14. /// </summary>
  15. private static readonly SqlSugarScope SqlSugarScope = new SqlSugarScope(new ConnectionConfig()
  16. {
  17. ConnectionString = _config!.ConnectionString, //连接符字串
  18. DbType = _config.DbType, //数据库类型
  19. IsAutoCloseConnection = true //不设成true要手动close
  20. }, db =>
  21. {
  22. //(A)全局生效配置点
  23. //调试SQL事件,可以删掉
  24. db.Aop.OnLogExecuting = (sql, pars) =>
  25. {
  26. DbLog.DBEX(sql);
  27. //输出sql,查看执行sql
  28. //5.0.8.2 获取无参数化 SQL
  29. //UtilMethods.GetSqlString(DbType.SqlServer,sql,pars)
  30. };
  31. });
  32. /// <summary>
  33. /// 获取db链接
  34. /// </summary>
  35. /// <returns></returns>
  36. /// <exception cref="Exception"></exception>
  37. public static SqlSugarScope Db
  38. {
  39. get
  40. {
  41. if (_config == null) throw new Exception("请使用SetConfig方法写入数据库链接信息");
  42. return SqlSugarScope;
  43. }
  44. }
  45. }
  46. }