|
- using Furion;
- using Microsoft.Extensions.Caching.Distributed;
- using Newtonsoft.Json;
- using SqlSugar;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace BPA.MES.Base.Core
- {
- public class KepServerSqlsugar
- {
- /// <summary>
- /// 获基础信息库对象 (用IOC这块代码不能写到IOC里面)
- /// </summary>
- public static ISqlSugarClient KepDb
- {
- get
- {
- //如果是跨服务器分库,也需要动态配置的,因为库的IP会变
- //参考业务库用法
- string configId = "configId";
- var dbs = App.GetConfig<List<ConnectionConfig>>("ConnectionConfigs")[0];
- if (!Db.IsAnyConnection(configId))
- { //用非默认ConfigId进行测试
- //添加业务库只在当前上下文有效(原理:SqlSugarScope模式入门文档去看)
- Db.AddConnection(new ConnectionConfig()
- {
- ConfigId = configId,
- ConnectionString = dbs.ConnectionString,
- DbType = DbType.MySql,
- IsAutoCloseConnection = true,
- InitKeyType = InitKeyType.Attribute
- });
- }
- //原理说明
- //IsAnyConnection、AddConnection和GetConnection 都是Scope周期不同请求不会有影响
-
- var result = Db.GetConnection(configId);
-
- //可以给业务库result设置AOP和过滤滤器
-
- return result;
- }
- }
-
-
- //通过IOC获取注入的Db对象
- public static SqlSugarScope Db
- {
- get
- {
- //如果用Furion就是 App.GetService<ISqlSugarClient>();
- //IOC不会可以看文档: https://www.donet5.com/Doc/27/2563
- //var ihttp=你存储的Services.BuildServiceProvider().GetService<IHttpContextAccessor>();
- var obj = App.GetService<ISqlSugarClient>();
- return (SqlSugarScope)obj;
- }
- }
-
- }
- }
|