diff --git a/Src/Asp.Net/SqlServerTest/Demos/7_Filter.cs b/Src/Asp.Net/SqlServerTest/Demos/7_Filter.cs index 03cf32f76..366be8b24 100644 --- a/Src/Asp.Net/SqlServerTest/Demos/7_Filter.cs +++ b/Src/Asp.Net/SqlServerTest/Demos/7_Filter.cs @@ -69,7 +69,7 @@ namespace OrmTest.Demo }); //Processing prior to execution of SQL - db.Ado.ProcessingEventStartingSQL = (sql, par) => + db.ProcessingEventStartingSQL = (sql, par) => { if (sql.Contains("{0}")) { diff --git a/Src/Asp.Net/SqlServerTest/Demos/K_MultiClient.cs b/Src/Asp.Net/SqlServerTest/Demos/K_MultiClient.cs new file mode 100644 index 000000000..3328fba56 --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/Demos/K_MultiClient.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace OrmTest.Demos +{ + class MultiClient + { + } +} diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj index d623e5094..3deac31e6 100644 --- a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj +++ b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj @@ -66,6 +66,7 @@ + diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/Mapping .cs b/Src/Asp.Net/SqlServerTest/UnitTest/Mapping .cs index b5be0d75c..54f1872ab 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/Mapping .cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/Mapping .cs @@ -58,7 +58,7 @@ namespace OrmTest.UnitTest { SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { InitKeyType = InitKeyType.Attribute, ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true }); db.Ado.IsEnableLogEvent = true; - db.Ado.LogEventStarting = (sql, pars) => + db.LogEventStarting = (sql, pars) => { Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars)); Console.WriteLine(); diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/Query/JoinQuery.cs b/Src/Asp.Net/SqlServerTest/UnitTest/Query/JoinQuery.cs index 60f3fb2c1..2a9b3caec 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/Query/JoinQuery.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/Query/JoinQuery.cs @@ -130,7 +130,7 @@ namespace OrmTest.UnitTest { SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer }); db.Ado.IsEnableLogEvent = true; - db.Ado.LogEventStarting = (sql, pars) => + db.LogEventStarting = (sql, pars) => { Console.WriteLine(sql + " " + pars); }; diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/Query/SelectQuery.cs b/Src/Asp.Net/SqlServerTest/UnitTest/Query/SelectQuery.cs index f9d1ffcc3..38b0a3193 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/Query/SelectQuery.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/Query/SelectQuery.cs @@ -30,7 +30,7 @@ namespace OrmTest.UnitTest using (var db = GetInstance()) { //db.Database.IsEnableLogEvent = true; - db.Ado.LogEventStarting = (sql, pars) => + db.LogEventStarting = (sql, pars) => { Console.WriteLine(sql + " " + pars); }; @@ -82,7 +82,7 @@ namespace OrmTest.UnitTest db.Ado.IsEnableLogEvent = true; - db.Ado.LogEventStarting = (sql, pars) => + db.LogEventStarting = (sql, pars) => { base.Check(" SELECT COUNT(1) FROM (SELECT [st].[ID] FROM [STudent] st Left JOIN [School] sc ON ( [st].[SchoolId] = [sc].[Id] ) Left JOIN [School] sc2 ON ( [sc2].[Id] = [sc].[Id] ) GROUP BY [st].[ID] ) CountTable ", null, sql, null, "select t4 Error"); diff --git a/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs index ddd1b6639..b8294fc64 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs @@ -59,12 +59,12 @@ namespace SqlSugar public virtual CommandType CommandType { get; set; } public virtual bool IsEnableLogEvent { get; set; } public virtual bool IsClearParameters { get; set; } - public virtual Action LogEventStarting { get; set; } - public virtual Action LogEventCompleted { get; set; } - public virtual Func> ProcessingEventStartingSQL { get; set; } + public virtual Action LogEventStarting=> this.Context.LogEventStarting; + public virtual Action LogEventCompleted => this.Context.LogEventCompleted; + public virtual Func> ProcessingEventStartingSQL => this.Context.ProcessingEventStartingSQL; protected virtual Func FormatSql { get; set; } - public virtual Action ErrorEvent { get; set; } - public virtual Action DiffLogEvent { get; set; } + public virtual Action ErrorEvent => this.Context.ErrorEvent; + public virtual Action DiffLogEvent => this.Context.DiffLogEvent; public virtual List SlaveConnections { get; set; } public virtual IDbConnection MasterConnection { get; set; } #endregion diff --git a/Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs index 716b6ca68..74d6dd519 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs @@ -14,10 +14,10 @@ namespace SqlSugar this.Context.Ado.IsEnableLogEvent = true; } private SqlSugarContext Context { get; set; } - public Action OnDiffLogEvent { set { this.Context.Ado.DiffLogEvent = value; } } - public Action OnError { set { this.Context.Ado.ErrorEvent = value; } } - public Action OnLogExecuting { set { this.Context.Ado.LogEventStarting = value; } } - public Action OnLogExecuted { set { this.Context.Ado.LogEventCompleted = value; } } - public Func> OnExecutingChangeSql { set { this.Context.Ado.ProcessingEventStartingSQL = value; } } + public Action OnDiffLogEvent { set { this.Context.DiffLogEvent = value; } } + public Action OnError { set { this.Context.ErrorEvent = value; } } + public Action OnLogExecuting { set { this.Context.LogEventStarting = value; } } + public Action OnLogExecuted { set { this.Context.LogEventCompleted = value; } } + public Func> OnExecutingChangeSql { set { this.Context.ProcessingEventStartingSQL = value; } } } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs index 63ceae534..7f9897f3d 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs @@ -410,8 +410,8 @@ namespace SqlSugar parameters = new List(); diffModel.AfterData = null; diffModel.Time = this.Context.Ado.SqlExecutionTime; - if (this.Context.Ado.DiffLogEvent != null) - this.Context.Ado.DiffLogEvent(diffModel); + if (this.Context.DiffLogEvent != null) + this.Context.DiffLogEvent(diffModel); this.Context.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation; } if (this.RemoveCacheFunc != null) { diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index 692686ec4..d95bb06f0 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -10,7 +10,7 @@ namespace SqlSugar { public class InsertableProvider : IInsertable where T : class, new() { - public ISqlSugarClient Context { get; set; } + public SqlSugarContext Context { get; set; } public IAdo Ado { get { return Context.Ado; } } public ISqlBuilder SqlBuilder { get; set; } public InsertBuilder InsertBuilder { get; set; } @@ -477,8 +477,8 @@ namespace SqlSugar parameters = new List(); diffModel.AfterData = GetDiffTable(sql, result); diffModel.Time = this.Context.Ado.SqlExecutionTime; - if (this.Context.Ado.DiffLogEvent != null) - this.Context.Ado.DiffLogEvent(diffModel); + if (this.Context.DiffLogEvent != null) + this.Context.DiffLogEvent(diffModel); this.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation; } if (this.RemoveCacheFunc != null) diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index 382128c55..51b3bff27 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -690,8 +690,8 @@ namespace SqlSugar parameters = new List(); diffModel.AfterData = GetDiffTable(sql, parameters); diffModel.Time = this.Context.Ado.SqlExecutionTime; - if (this.Context.Ado.DiffLogEvent != null) - this.Context.Ado.DiffLogEvent(diffModel); + if (this.Context.DiffLogEvent != null) + this.Context.DiffLogEvent(diffModel); this.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation; } if (this.RemoveCacheFunc != null) diff --git a/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs b/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs index 7bad65dea..cee77e19d 100644 --- a/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs +++ b/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs @@ -278,9 +278,9 @@ namespace SqlSugar if (isCopyEvents) { newClient.Ado.IsEnableLogEvent = Context.Ado.IsEnableLogEvent; - newClient.Ado.LogEventStarting = Context.Ado.LogEventStarting; - newClient.Ado.LogEventCompleted = Context.Ado.LogEventCompleted; - newClient.Ado.ProcessingEventStartingSQL = Context.Ado.ProcessingEventStartingSQL; + newClient.LogEventStarting = Context.LogEventStarting; + newClient.LogEventCompleted = Context.LogEventCompleted; + newClient.ProcessingEventStartingSQL = Context.ProcessingEventStartingSQL; newClient.QueryFilter = Context.QueryFilter; } return newClient; diff --git a/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs b/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs index 91f935bff..16e2fc1b8 100644 --- a/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs +++ b/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs @@ -31,6 +31,12 @@ namespace SqlSugar public MappingColumnList MappingColumns { get; set; } public IgnoreColumnList IgnoreColumns { get; set; } public IgnoreColumnList IgnoreInsertColumns { get; set; } + public Action LogEventStarting { get; set; } + public Action LogEventCompleted { get; set; } + public Func> ProcessingEventStartingSQL { get; set; } + public Action ErrorEvent { get; set; } + public Action DiffLogEvent { get; set; } + public QueueList _Queues = new QueueList(); #endregion #region Fields @@ -127,7 +133,7 @@ namespace SqlSugar } #endregion - internal void InitMppingInfo() + public void InitMppingInfo() { InitMppingInfo(typeof(T)); } diff --git a/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarContext.cs b/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarContext.cs index 624101de0..71ff9e911 100644 --- a/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarContext.cs +++ b/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarContext.cs @@ -912,7 +912,7 @@ namespace SqlSugar } this.Queues.Add(sql, parsmeters); } - public QueueList Queues { get; set; } + public QueueList Queues { get =>_Queues; set => _Queues = value; } private T SaveQueuesProvider(bool isTran, Func, T> func) { diff --git a/Src/Asp.Net/SqlSugar/Interface/IAdo.cs b/Src/Asp.Net/SqlSugar/Interface/IAdo.cs index 072767c7c..46cd8e7ab 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IAdo.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IAdo.cs @@ -24,11 +24,6 @@ namespace SqlSugar CommandType CommandType { get; set; } bool IsEnableLogEvent { get; set; } bool IsDisableMasterSlaveSeparation { get; set; } - Action LogEventStarting { get; set; } - Action LogEventCompleted { get; set; } - Func> ProcessingEventStartingSQL { get; set; } - Action ErrorEvent { get; set; } - Action DiffLogEvent { get; set; } bool IsClearParameters { get; set; } int CommandTimeOut { get; set; } TimeSpan SqlExecutionTime { get; } diff --git a/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs b/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs index 860a08fd7..277321eaa 100644 --- a/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs +++ b/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs @@ -12,6 +12,11 @@ namespace SqlSugar MappingColumnList MappingColumns { get; set; } IgnoreColumnList IgnoreColumns { get; set; } IgnoreColumnList IgnoreInsertColumns { get; set; } + Action LogEventStarting { get; set; } + Action LogEventCompleted { get; set; } + Func> ProcessingEventStartingSQL { get; set; } + Action ErrorEvent { get; set; } + Action DiffLogEvent { get; set; } QueueList Queues { get; set; } IAdo Ado { get; } AopProvider Aop { get; } @@ -47,6 +52,7 @@ namespace SqlSugar SimpleClient GetSimpleClient(); SimpleClient GetSimpleClient() where T : class, new(); void InitMppingInfo(Type type); + void InitMppingInfo(); IInsertable Insertable(Dictionary columnDictionary) where T : class, new(); IInsertable Insertable(dynamic insertDynamicObject) where T : class, new(); IInsertable Insertable(List insertObjs) where T : class, new(); diff --git a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs index d14947ec2..586ad2241 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs @@ -11,13 +11,25 @@ namespace SqlSugar { public class SqlSugarClient : ISqlSugarClient { + #region Gobal Property private ISqlSugarClient _Context = null; - private string ThreadId; + private string _ThreadId; private ConnectionConfig _CurrentConnectionConfig; - private List _allClients; + private List _AllClients; + private bool _IsAllTran = false; + private MappingTableList _MappingTables; + private MappingColumnList _MappingColumns; + private IgnoreColumnList _IgnoreColumns; + private IgnoreColumnList _IgnoreInsertColumns; + private Action _LogEventStarting; + private Action _LogEventCompleted; + private Func> _ProcessingEventStartingSQL; + private Action _ErrorEvent; + private Action _DiffLogEvent; + #endregion + #region Api public ISqlSugarClient Context { get => GetContext(); set => _Context = value; } - public SqlSugarClient(ConnectionConfig config) { Check.Exception(config == null, "ConnectionConfig config is null"); @@ -30,23 +42,45 @@ namespace SqlSugar InitConfigs(configs); var config = configs.First(); InitContext(config); - _allClients = configs.Select(it => new SugarTerant() { ConnectionConfig = it }).ToList(); ; - _allClients.First(it => it.ConnectionConfig.ConfigId == config.ConfigId).Context = this.Context; + _AllClients = configs.Select(it => new SugarTerant() { ConnectionConfig = it }).ToList(); ; + _AllClients.First(it => it.ConnectionConfig.ConfigId == config.ConfigId).Context = this.Context; } - public void ChangeDatabase(string configId) { - Check.Exception(!_allClients.Any(it => it.ConnectionConfig.ConfigId == configId), "ConfigId was not found {0}", configId); - InitTerant(_allClients.First(it => it.ConnectionConfig.ConfigId == configId)); + Check.Exception(!_AllClients.Any(it => it.ConnectionConfig.ConfigId == configId), "ConfigId was not found {0}", configId); + InitTerant(_AllClients.First(it => it.ConnectionConfig.ConfigId == configId)); + if (this._IsAllTran) + this.Ado.BeginTran(); } public void ChangeDatabase(Func changeExpression) { - var allConfigs = _allClients.Select(it => it.ConnectionConfig); + var allConfigs = _AllClients.Select(it => it.ConnectionConfig); Check.Exception(!allConfigs.Any(changeExpression), "changeExpression was not found {0}", changeExpression.ToString()); InitContext(allConfigs.First(changeExpression)); + if (this._IsAllTran) + this.Ado.BeginTran(); } + + public MappingTableList MappingTables { get => _MappingTables; set => _MappingTables = value; } + public MappingColumnList MappingColumns { get => _MappingColumns; set => _MappingColumns = value; } + public IgnoreColumnList IgnoreColumns { get => _IgnoreColumns; set => _IgnoreColumns = value; } + public IgnoreColumnList IgnoreInsertColumns { get => _IgnoreInsertColumns; set => _IgnoreInsertColumns = value; } + public Action LogEventStarting { get => _LogEventStarting; set => _LogEventStarting = value; } + public Action LogEventCompleted { get => _LogEventCompleted; set => _LogEventCompleted = value; } + public Func> ProcessingEventStartingSQL { get => _ProcessingEventStartingSQL; set => _ProcessingEventStartingSQL = value; } + public Action ErrorEvent { get => _ErrorEvent; set => _ErrorEvent = value; } + public Action DiffLogEvent { get => _DiffLogEvent; set => _DiffLogEvent = value; } + public ConnectionConfig CurrentConnectionConfig { get => _CurrentConnectionConfig; set => _CurrentConnectionConfig = value; } + + + + + public QueueList Queues { get => this.Context.Queues; set => this.Context.Queues = value; } + + public Dictionary TempItems { get => this.Context.TempItems??new Dictionary(); set => this.Context.TempItems = value; } + public IContextMethods Utilities { get => this.Context.Utilities; set => this.Context.Utilities = value; } public IAdo Ado => this.Context.Ado; public AopProvider Aop => this.Context.Aop; @@ -54,7 +88,7 @@ namespace SqlSugar public ICodeFirst CodeFirst => this.Context.CodeFirst; public Guid ContextID { get => this.Context.ContextID; set => this.Context.ContextID = value; } - public ConnectionConfig CurrentConnectionConfig { get => _CurrentConnectionConfig; set => _CurrentConnectionConfig = value; } + public IDbFirst DbFirst => this.Context.DbFirst; @@ -72,14 +106,6 @@ namespace SqlSugar [Obsolete] public SimpleClient SimpleClient => this.Context.SimpleClient; - public Dictionary TempItems { get => this.Context.TempItems; set => this.Context.TempItems = value; } - public IContextMethods Utilities { get => this.Context.Utilities; set => this.Context.Utilities = value; } - public MappingTableList MappingTables { get => this.Context.MappingTables; set => this.Context.MappingTables = value; } - public MappingColumnList MappingColumns { get => this.Context.MappingColumns; set => this.Context.MappingColumns = value; } - public IgnoreColumnList IgnoreColumns { get => this.Context.IgnoreColumns; set => this.Context.IgnoreColumns = value; } - public IgnoreColumnList IgnoreInsertColumns { get => this.Context.IgnoreInsertColumns; set => this.Context.IgnoreInsertColumns = value; } - public QueueList Queues { get => this.Context.Queues; set => this.Context.Queues = value; } - public void AddQueue(string sql, object parsmeters = null) { this.Context.AddQueue(sql, parsmeters); @@ -159,6 +185,10 @@ namespace SqlSugar { this.Context.InitMppingInfo(type); } + public void InitMppingInfo() + { + this.Context.InitMppingInfo(typeof(T)); + } public IInsertable Insertable(Dictionary columnDictionary) where T : class, new() { @@ -544,66 +574,134 @@ namespace SqlSugar return this.Context.Updateable(UpdateObjs); } + public void BeginAllTran() + { + _IsAllTran = true; + this.Context.Ado.BeginTran(); + } + public void CommitAllTran() + { + if (_AllClients.HasValue()) + { + foreach (var item in _AllClients.Where(it => it.Context.HasValue())) + { + item.Context.Ado.CommitTran(); + } + } + _IsAllTran = false; + } + public void RollbackAllTran() + { + if (_AllClients.HasValue()) + { + foreach (var item in _AllClients.Where(it => it.Context.HasValue())) + { + item.Context.Ado.RollbackTran(); + } + } + _IsAllTran = false; + } + #endregion + + #region Helper private ISqlSugarClient GetContext() { if (CurrentConnectionConfig.IsShardSameThread) { - var result = _Context; + ISqlSugarClient result = _Context; if (CallContext.ContextList.Value.IsNullOrEmpty()) { + CallContext.ContextList.Value = new List(); CallContext.ContextList.Value.Add(_Context); } else { - var cacheContext = CallContext.ContextList.Value.FirstOrDefault(it => - it.CurrentConnectionConfig.ConnectionString == _Context.CurrentConnectionConfig.ConnectionString && - it.CurrentConnectionConfig.DbType == _Context.CurrentConnectionConfig.DbType && - it.CurrentConnectionConfig.IsAutoCloseConnection == _Context.CurrentConnectionConfig.IsAutoCloseConnection && - it.CurrentConnectionConfig.IsShardSameThread == _Context.CurrentConnectionConfig.IsShardSameThread); + ISqlSugarClient cacheContext = GetCallContext(); if (cacheContext != null) { - return cacheContext; + result = cacheContext; + } + else + { + result = CopyClient(); + CallContext.ContextList.Value.Add(result); } } return result; } - else if (ThreadId == Thread.CurrentThread.ManagedThreadId.ToString()) + else if (_ThreadId == Thread.CurrentThread.ManagedThreadId.ToString()) { + _Context.MappingColumns = _MappingColumns; + _Context.MappingTables = _MappingTables; + _Context.IgnoreColumns = _IgnoreColumns; + _Context.IgnoreInsertColumns = _IgnoreInsertColumns; + _Context.DiffLogEvent = _DiffLogEvent; + _Context.LogEventCompleted = _LogEventCompleted; + _Context.LogEventStarting = _LogEventStarting; + _Context.ErrorEvent = _ErrorEvent; + _Context.ProcessingEventStartingSQL = _ProcessingEventStartingSQL; return _Context; } else { - return new SqlSugarClient(this.CurrentConnectionConfig); + if (CallContext.ContextList.Value == null) + { + CallContext.ContextList.Value = new List(); + } + if (CallContext.ContextList.Value.IsNullOrEmpty() || GetCallContext() == null) + { + var context = CopyClient(); + CallContext.ContextList.Value.Add(context); + return context; + } + else + { + return GetCallContext(); + } } } + + private SqlSugarClient CopyClient() + { + var result = new SqlSugarClient(this.CurrentConnectionConfig); + result.MappingColumns = _MappingColumns; + result.MappingTables = _MappingTables; + result.IgnoreColumns = _IgnoreColumns; + result.IgnoreInsertColumns = _IgnoreInsertColumns; + result.DiffLogEvent = _DiffLogEvent; + result.LogEventCompleted = _LogEventCompleted; + result.LogEventStarting = _LogEventStarting; + result.ErrorEvent = _ErrorEvent; + result.ProcessingEventStartingSQL = _ProcessingEventStartingSQL; + return result; + } + + private ISqlSugarClient GetCallContext() + { + return CallContext.ContextList.Value.FirstOrDefault(it => + it.CurrentConnectionConfig.DbType == _Context.CurrentConnectionConfig.DbType&& + it.CurrentConnectionConfig.ConnectionString == _Context.CurrentConnectionConfig.ConnectionString&& + it.CurrentConnectionConfig.InitKeyType==_Context.CurrentConnectionConfig.InitKeyType + ); + } + private void InitContext(ConnectionConfig config) { _Context = new SqlSugarContext(config); this.CurrentConnectionConfig = config; - ThreadId = Thread.CurrentThread.ManagedThreadId.ToString(); - if (this.MappingTables == null) - { + _ThreadId = Thread.CurrentThread.ManagedThreadId.ToString(); + if (_MappingColumns == null) this.MappingTables = new MappingTableList(); - } if (this.MappingColumns == null) - { this.MappingColumns = new MappingColumnList(); - } if (this.IgnoreColumns == null) - { this.IgnoreColumns = new IgnoreColumnList(); - } if (this.IgnoreInsertColumns == null) - { this.IgnoreInsertColumns = new IgnoreColumnList(); - } - if (this.Queues == null) - { - this.Queues = new QueueList(); - } } + private void InitConfigs(List configs) { foreach (var item in configs) @@ -614,6 +712,7 @@ namespace SqlSugar } } } + private void InitTerant(SugarTerant terant) { if (terant.Context == null) @@ -622,6 +721,7 @@ namespace SqlSugar } _Context = terant.Context; this.CurrentConnectionConfig = terant.ConnectionConfig; - } + } + #endregion } }