From 48b608b2929ddd4770097333714d3525db5940bc Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Wed, 27 Apr 2022 18:10:13 +0800 Subject: [PATCH] Update db.GetConnectionScope --- .../SugarProvider/SqlSugarScopeProvider.cs | 65 +++++++++++++------ 1 file changed, 46 insertions(+), 19 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs index 2d9c3a482..47b1a0052 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs @@ -18,31 +18,58 @@ namespace SqlSugar { this.conn = conn; var key = GetKey(); - CallContextAsync.SetData(key, conn); + this.GetContext(true); } public SqlSugarProvider ScopedContext { get { return GetContext(); } } - private SqlSugarProvider GetAsyncContext(string key) + private SqlSugarProvider GetAsyncContext(bool isInit=false) { - SqlSugarProvider result = CallContextAsync.GetData(key); - if (result == null) + if (isInit) { - CallContextAsync.SetData(key, new SqlSugarProvider(conn.CurrentConnectionConfig)); - result = CallContextAsync.GetData(key); + CallContextAsync.SetData(GetKey(), this.conn); + isInit = false; + return conn; } + else + { + SqlSugarProvider result = CallContextAsync.GetData(GetKey()); + if (result == null) + { + SqlSugarProvider db=new SqlSugarProvider(this.conn.CurrentConnectionConfig); + CallContextAsync.SetData(GetKey(), db); + return db; + } + else + { - return result; - } - private SqlSugarProvider GetThreadContext(string key) - { - SqlSugarProvider result = CallContextThread.GetData(key); - if (result == null) - { - CallContextThread.SetData(key, new SqlSugarProvider (this.CurrentConnectionConfig)); - result = CallContextThread.GetData(key); + return result; + } } - return result; } - private SqlSugarProvider GetContext() + private SqlSugarProvider GetThreadContext(bool isInit = false) + { + if (isInit) + { + CallContextThread.SetData(GetKey(), this.conn); + isInit = false; + return conn; + } + else + { + SqlSugarProvider result = CallContextThread.GetData(GetKey()); + if (result == null) + { + SqlSugarProvider db = new SqlSugarProvider(this.conn.CurrentConnectionConfig); + CallContextThread.SetData(GetKey(), db); + return db; + } + else + { + + return result; + } + } + } + private SqlSugarProvider GetContext(bool isInit = false) { SqlSugarProvider result = null; var key = GetKey(); ; @@ -51,11 +78,11 @@ namespace SqlSugar var isAsync = UtilMethods.IsAnyAsyncMethod(methods); if (isAsync) { - result= GetAsyncContext(key); + result= GetAsyncContext(isInit); } else { - result= GetThreadContext(key); + result= GetThreadContext(isInit); } return result; }