From 4691b232040b62e7db8e90331af81a79ed0c9ccc Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Fri, 25 Nov 2022 15:50:45 +0800 Subject: [PATCH] Add RemoveConnection --- Src/Asp.NetCore2/SqlSugar/Interface/ITenant.cs | 1 + Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs | 17 +++++++++++++++++ Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs | 4 ++++ 3 files changed, 22 insertions(+) diff --git a/Src/Asp.NetCore2/SqlSugar/Interface/ITenant.cs b/Src/Asp.NetCore2/SqlSugar/Interface/ITenant.cs index 3ea2ee34e..c5852b2b0 100644 --- a/Src/Asp.NetCore2/SqlSugar/Interface/ITenant.cs +++ b/Src/Asp.NetCore2/SqlSugar/Interface/ITenant.cs @@ -21,6 +21,7 @@ namespace SqlSugar Task> UseTranAsync(Func> action, Action errorCallBack = null); void AddConnection(ConnectionConfig connection); SqlSugarProvider GetConnection(dynamic configId); + void RemoveConnection(string configId); SqlSugarScopeProvider GetConnectionScope(dynamic configId); SqlSugarProvider GetConnectionWithAttr(); SqlSugarScopeProvider GetConnectionScopeWithAttr(); diff --git a/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs b/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs index bccc1ec38..b6a966625 100644 --- a/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs @@ -777,6 +777,23 @@ namespace SqlSugar { return new SqlSugarTransaction(this); } + public void RemoveConnection(string configId) + { + var removeData= this._AllClients.FirstOrDefault(it => it.ConnectionConfig.ConfigId.ObjToString()== configId.ObjToString()); + var currentId= this.CurrentConnectionConfig.ConfigId; + if (removeData != null) + { + if (removeData.Context.Ado.IsAnyTran()) + { + Check.ExceptionEasy("RemoveConnection error has tran",$"删除失败{removeData.ConnectionConfig.ConfigId}存在未提交事务"); + } + else if (removeData.ConnectionConfig.ConfigId.ObjToString()== currentId.ObjToString()) + { + Check.ExceptionEasy("Default ConfigId cannot be deleted", $"默认库不能删除{removeData.ConnectionConfig.ConfigId}"); + } + this._AllClients.Remove(removeData); + } + } public void AddConnection(ConnectionConfig connection) { Check.ArgumentNullException(connection, "AddConnection.connection can't be null"); diff --git a/Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs b/Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs index 9291acc11..0cc3489d0 100644 --- a/Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs +++ b/Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs @@ -793,5 +793,9 @@ namespace SqlSugar { ScopedContext.Tracking(datas); } + public void RemoveConnection(string configId) + { + ScopedContext.RemoveConnection(configId); + } } }