using SqlSugar; using System; using System.Collections.Generic; using System.Text; using EntityRiskUiBackend.Application; using System.Linq; using EntityRiskUiBackend.Entity.DTO; using EntityInfo = EntityRiskUiBackend.Entity.DTO.EntityInfo; using EntityRiskUiBackend.Repos.SqlSugar.Extensions.DataCache;
namespace EntityRiskUiBackend.Repos.DB { public class SqlSugarDbContext { public SqlSugarDbContext() { Db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = EntityRiskUiBackend.Application.Config.DBConfig.DBConnectionString, DbType = DbType.SqlServer, InitKeyType = InitKeyType.SystemTable,//从特性读取主键和自增列信息 IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了 ConfigureExternalServices = new ConfigureExternalServices() { DataInfoCacheService = new MemoryCache() //MemoryCache是继承ICacheService自已实现的一个类 } }); //调式代码 用来打印SQL Db.Aop.OnLogExecuting = (sql, pars) => { Console.WriteLine(sql + "\r\n" + Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); Console.WriteLine(); };
Db.Aop.OnDiffLogEvent = it => { var editBeforeData = it.BeforeData; var editAfterData = it.AfterData; var sql = it.Sql; var parameter = it.Parameters; var data = it.BusinessData; var time = it.Time; var diffType = it.DiffType;//枚举值 insert 、update 和 delete 用来作业务区分 Console.WriteLine($" 记录执行的日志 进行审计 {editBeforeData} {editAfterData} {sql} {parameter} {data} {time} {diffType}"); //你可以在这里面写日志方法 }; } //注意:不能写成静态的,不能写成静态的 public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作
public SimpleClient<Enterpris> EnterprisDb { get { return new SimpleClient<Enterpris>(Db); } }
public SimpleClient<EntityInfo> EntityInfoDb { get { return new SimpleClient<EntityInfo>(Db); } } public SimpleClient<CreditChinaEntityBasicInfo> CreditChinaEntityBasicInfoDb { get { return new SimpleClient<CreditChinaEntityBasicInfo>(Db); } } public SimpleClient<SystemUsers> SystemUserDb { get { return new SimpleClient<SystemUsers>(Db); } }
public SimpleClient<News> NewsDb { get { return new SimpleClient<News>(Db); } } public SimpleClient<RiskFactor> RiskFactorDb { get { return new SimpleClient<RiskFactor>(Db); } } public SimpleClient<RiskUiJobHistory> RiskUiJobHistoryDb { get { return new SimpleClient<RiskUiJobHistory>(Db); } } } }
缓存
using EntityRiskUiBackend.Application.Helper; using SqlSugar; using System; using System.Collections.Generic; using System.Text;
namespace EntityRiskUiBackend.Repos.SqlSugar.Extensions.DataCache { class MemoryCache : ICacheService { public void Add<V>(string key, V value) { MemoryCacheHelper<V>.GetInstance().Add(key, value); }
public void Add<V>(string key, V value, int cacheDurationInSeconds) { MemoryCacheHelper<V>.GetInstance().Add(key, value, cacheDurationInSeconds); }
public bool ContainsKey<V>(string key) { return MemoryCacheHelper<V>.GetInstance().ContainsKey(key); }
public V Get<V>(string key) { return MemoryCacheHelper<V>.GetInstance().Get(key); }
public IEnumerable<string> GetAllKey<V>() { throw new NotImplementedException(); }
public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue) { var cacheManager = MemoryCacheHelper<V>.GetInstance(); if (cacheManager.ContainsKey(cacheKey)) { return cacheManager[cacheKey]; } else { var result = create(); cacheManager.Add(cacheKey, result, cacheDurationInSeconds); return result; } }
public void Remove<V>(string key) { MemoryCacheHelper<V>.GetInstance().Remove(key); } } }