集成,总结MES功能
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.
 
 
 
 

65 lines
2.2 KiB

  1. using Furion;
  2. using Microsoft.Extensions.Caching.Distributed;
  3. using Newtonsoft.Json;
  4. using SqlSugar;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace BPA.MES.Base.Core
  11. {
  12. public class KepServerSqlsugar
  13. {
  14. /// <summary>
  15. /// 获基础信息库对象 (用IOC这块代码不能写到IOC里面)
  16. /// </summary>
  17. public static ISqlSugarClient KepDb
  18. {
  19. get
  20. {
  21. //如果是跨服务器分库,也需要动态配置的,因为库的IP会变
  22. //参考业务库用法
  23. string configId = "configId";
  24. var dbs = App.GetConfig<List<ConnectionConfig>>("ConnectionConfigs")[0];
  25. if (!Db.IsAnyConnection(configId))
  26. { //用非默认ConfigId进行测试
  27. //添加业务库只在当前上下文有效(原理:SqlSugarScope模式入门文档去看)
  28. Db.AddConnection(new ConnectionConfig()
  29. {
  30. ConfigId = configId,
  31. ConnectionString = dbs.ConnectionString,
  32. DbType = DbType.MySql,
  33. IsAutoCloseConnection = true,
  34. InitKeyType = InitKeyType.Attribute
  35. });
  36. }
  37. //原理说明
  38. //IsAnyConnection、AddConnection和GetConnection 都是Scope周期不同请求不会有影响
  39. var result = Db.GetConnection(configId);
  40. //可以给业务库result设置AOP和过滤滤器
  41. return result;
  42. }
  43. }
  44. //通过IOC获取注入的Db对象
  45. public static SqlSugarScope Db
  46. {
  47. get
  48. {
  49. //如果用Furion就是 App.GetService<ISqlSugarClient>();
  50. //IOC不会可以看文档: https://www.donet5.com/Doc/27/2563
  51. //var ihttp=你存储的Services.BuildServiceProvider().GetService<IHttpContextAccessor>();
  52. var obj = App.GetService<ISqlSugarClient>();
  53. return (SqlSugarScope)obj;
  54. }
  55. }
  56. }
  57. }