diff --git a/Src/Asp.Net/SqlSugar/SqlSugarScope.cs b/Src/Asp.Net/SqlSugar/SqlSugarScope.cs index 92c6248cc..98448daed 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugarScope.cs +++ b/Src/Asp.Net/SqlSugar/SqlSugarScope.cs @@ -34,62 +34,7 @@ namespace SqlSugar _configs = configs; this._configAction = configAction; } - //public ScopedClient(SqlSugarClient context,Action configAction) - //{ - // this.db = context; - // this.configAction = configAction; - //} - public SqlSugarClient ScopedContext - { - get - { - SqlSugarClient result = null; - var key = _configs.GetHashCode().ToString(); - StackTrace st = new StackTrace(true); - var methods = st.GetFrames(); - var isAsync = UtilMethods.IsAnyAsyncMethod(methods); - if (isAsync) - { - result=GetAsyncContext(key); - } - else - { - result = GetThreadContext(key); - } - return result; - } - } - - private SqlSugarClient GetAsyncContext(string key) - { - SqlSugarClient result = CallContextAsync.GetData(key); - if (result == null) - { - CallContextAsync.SetData(key, new SqlSugarClient(_configs)); - result = CallContextAsync.GetData(key); - if (this._configAction != null) - { - this._configAction(result); - } - } - - return result; - } - - private SqlSugarClient GetThreadContext(string key) - { - SqlSugarClient result = CallContextThread.GetData(key); - if (result == null) - { - CallContextThread.SetData(key, new SqlSugarClient(_configs)); - result = CallContextThread.GetData(key); - if (this._configAction != null) - { - this._configAction(result); - } - } - return result; - } + public SqlSugarClient ScopedContext{ get{ return GetContext();}} public MappingTableList MappingTables { get => ScopedContext.MappingTables; set => ScopedContext.MappingTables = value; } public MappingColumnList MappingColumns { get => ScopedContext.MappingColumns; set => ScopedContext.MappingColumns=value; } @@ -679,5 +624,54 @@ namespace SqlSugar { return ScopedContext.IsAnyConnection(configId); } + + private SqlSugarClient GetContext() + { + SqlSugarClient result = null; + var key = _configs.GetHashCode().ToString(); + StackTrace st = new StackTrace(true); + var methods = st.GetFrames(); + var isAsync = UtilMethods.IsAnyAsyncMethod(methods); + if (isAsync) + { + result = GetAsyncContext(key); + } + else + { + result = GetThreadContext(key); + } + return result; + } + + private SqlSugarClient GetAsyncContext(string key) + { + SqlSugarClient result = CallContextAsync.GetData(key); + if (result == null) + { + CallContextAsync.SetData(key, new SqlSugarClient(_configs)); + result = CallContextAsync.GetData(key); + if (this._configAction != null) + { + this._configAction(result); + } + } + + return result; + } + + private SqlSugarClient GetThreadContext(string key) + { + SqlSugarClient result = CallContextThread.GetData(key); + if (result == null) + { + CallContextThread.SetData(key, new SqlSugarClient(_configs)); + result = CallContextThread.GetData(key); + if (this._configAction != null) + { + this._configAction(result); + } + } + return result; + } } }