From fca2a2d59d4673602b200a3318d69fe732d755ab Mon Sep 17 00:00:00 2001 From: sunkaixuna <610262374@qq.com> Date: Thu, 16 Dec 2021 01:15:16 +0800 Subject: [PATCH] Optimized Code --- Src/Asp.Net/SqlSugar/SqlSugarScope.cs | 51 ++++++++++++++++----------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/SqlSugarScope.cs b/Src/Asp.Net/SqlSugar/SqlSugarScope.cs index 35b547ba6..4557c5122 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugarScope.cs +++ b/Src/Asp.Net/SqlSugar/SqlSugarScope.cs @@ -646,6 +646,8 @@ namespace SqlSugar { return ScopedContext.Fastest(); } + + private SqlSugarClient GetContext() { SqlSugarClient result = null; @@ -669,7 +671,8 @@ namespace SqlSugar SqlSugarClient result = CallContextAsync.GetData(key); if (result == null) { - CallContextAsync.SetData(key, new SqlSugarClient(_configs)); + List configList = GetCopyConfigs(); + CallContextAsync.SetData(key, new SqlSugarClient(configList)); result = CallContextAsync.GetData(key); if (this._configAction != null) { @@ -685,25 +688,8 @@ namespace SqlSugar SqlSugarClient result = CallContextThread.GetData(key); if (result == null) { - CallContextThread.SetData(key, new SqlSugarClient(_configs.Select(it=>new ConnectionConfig() { - AopEvents=it.AopEvents, - ConfigId=it.ConfigId, - ConfigureExternalServices=it.ConfigureExternalServices, - ConnectionString=it.ConnectionString, - DbType=it.DbType, - IndexSuffix=it.IndexSuffix, - InitKeyType=it.InitKeyType, - IsAutoCloseConnection=it.IsAutoCloseConnection, - LanguageType=it.LanguageType, - MoreSettings=it.MoreSettings==null?null:new ConnMoreSettings() { - DefaultCacheDurationInSeconds=it.MoreSettings.DefaultCacheDurationInSeconds, - DisableNvarchar=it.MoreSettings.DisableNvarchar, - PgSqlIsAutoToLower=it.MoreSettings.PgSqlIsAutoToLower, - IsAutoRemoveDataCache=it.MoreSettings.IsAutoRemoveDataCache, - IsWithNoLockQuery=it.MoreSettings.IsWithNoLockQuery - }, - SlaveConnectionConfigs=it.SlaveConnectionConfigs - }).ToList())); + List configList = GetCopyConfigs(); + CallContextThread.SetData(key, new SqlSugarClient(configList)); result = CallContextThread.GetData(key); if (this._configAction != null) { @@ -712,5 +698,30 @@ namespace SqlSugar } return result; } + + private List GetCopyConfigs() + { + return _configs.Select(it => new ConnectionConfig() + { + AopEvents = it.AopEvents, + ConfigId = it.ConfigId, + ConfigureExternalServices = it.ConfigureExternalServices, + ConnectionString = it.ConnectionString, + DbType = it.DbType, + IndexSuffix = it.IndexSuffix, + InitKeyType = it.InitKeyType, + IsAutoCloseConnection = it.IsAutoCloseConnection, + LanguageType = it.LanguageType, + MoreSettings = it.MoreSettings == null ? null : new ConnMoreSettings() + { + DefaultCacheDurationInSeconds = it.MoreSettings.DefaultCacheDurationInSeconds, + DisableNvarchar = it.MoreSettings.DisableNvarchar, + PgSqlIsAutoToLower = it.MoreSettings.PgSqlIsAutoToLower, + IsAutoRemoveDataCache = it.MoreSettings.IsAutoRemoveDataCache, + IsWithNoLockQuery = it.MoreSettings.IsWithNoLockQuery + }, + SlaveConnectionConfigs = it.SlaveConnectionConfigs + }).ToList(); + } } }