using SqlSugar; namespace ServiceCenter.SqlSugars { /// /// ORM /// public class SqlSugarHelper { /// /// 数据库连接 /// private static SqlSugarScope? _Db { get; set; } = null; /// /// 默认数据库连接Key /// private static string? _Default { get; set; } = null; /// /// Dlog数据库连接Key /// private static string? _Dlog { get; set; } = null; /// /// 设置数据库连接Key /// /// 默认多租户ID public static void SetDefault(string configId) { _Default = configId; } /// /// 设置 Dlog数据库连接Key /// /// 多租户Dlog ID public static void SetDlog(string configId) { _Dlog = configId; } /// /// 默认数据库连接Key /// public static string Default { get { return _Default ?? throw new Exception("请调用[SqlSugarHelper.SetDefault]方法设置默认数据库连接"); } } /// /// Dlog数据库连接Key /// public static string Dlog { get { return _Dlog ?? throw new Exception("请调用[SqlSugarHelper.SetDlog]方法设置Dlog数据库连接"); } } /// /// 设置数据库连接 /// /// public static void SetDb(SqlSugarScope sqlSugarScope) { _Db = sqlSugarScope; } /// /// 数据库连接 /// public SqlSugarScope Connect { get { return _Db ?? throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接"); } } /// /// 执行事务 /// /// /// public static void Do(Action act) { if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接"); var db = new SqlSugarHelper(); try { db.Connect.BeginTran();//开始事务 act(db);//执行委托 db.Connect.CommitTran();//提交事务 } catch (Exception ex) { db.Connect.RollbackTran();//回滚事务 throw new Exception($"事务回滚记录:{ex.Message}:{ex.StackTrace}"); } } } }