From 0e47c98431a7a9a7f3f3152d26fa832678c72697 Mon Sep 17 00:00:00 2001 From: sunkaixuna <610262374@qq.com> Date: Sun, 7 Nov 2021 00:38:08 +0800 Subject: [PATCH] SqlSugarScope MasterSlave BUG --- Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs | 4 +++- .../SqlSeverTest/SqlSugar/Abstract/AdoProvider/AdoProvider.cs | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs index 52a973b95..a48ebde4b 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs @@ -68,6 +68,7 @@ namespace SqlSugar public virtual Action DiffLogEvent => this.Context.CurrentConnectionConfig.AopEvents?.OnDiffLogEvent; public virtual List SlaveConnections { get; set; } public virtual IDbConnection MasterConnection { get; set; } + public virtual string MasterConnectionString { get; set; } public virtual CancellationToken? CancellationToken { get; set; } #endregion @@ -1373,6 +1374,7 @@ namespace SqlSugar if (this.MasterConnection == null) { this.MasterConnection = this.Connection; + this.MasterConnectionString = this.MasterConnection.ConnectionString; } var saves = this.Context.CurrentConnectionConfig.SlaveConnectionConfigs.Where(it => it.HitRate > 0).ToList(); var currentIndex = UtilRandom.GetRandomIndex(saves.ToDictionary(it => saves.ToList().IndexOf(it), it => it.HitRate)); @@ -1401,7 +1403,7 @@ namespace SqlSugar if (this.IsMasterSlaveSeparation && IsRead(sql) && this.Transaction == null) { this.Connection = this.MasterConnection; - this.Context.CurrentConnectionConfig.ConnectionString = this.MasterConnection.ConnectionString; + this.Context.CurrentConnectionConfig.ConnectionString = this.MasterConnectionString; } } diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/AdoProvider/AdoProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/AdoProvider/AdoProvider.cs index 52a973b95..a48ebde4b 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/AdoProvider/AdoProvider.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/AdoProvider/AdoProvider.cs @@ -68,6 +68,7 @@ namespace SqlSugar public virtual Action DiffLogEvent => this.Context.CurrentConnectionConfig.AopEvents?.OnDiffLogEvent; public virtual List SlaveConnections { get; set; } public virtual IDbConnection MasterConnection { get; set; } + public virtual string MasterConnectionString { get; set; } public virtual CancellationToken? CancellationToken { get; set; } #endregion @@ -1373,6 +1374,7 @@ namespace SqlSugar if (this.MasterConnection == null) { this.MasterConnection = this.Connection; + this.MasterConnectionString = this.MasterConnection.ConnectionString; } var saves = this.Context.CurrentConnectionConfig.SlaveConnectionConfigs.Where(it => it.HitRate > 0).ToList(); var currentIndex = UtilRandom.GetRandomIndex(saves.ToDictionary(it => saves.ToList().IndexOf(it), it => it.HitRate)); @@ -1401,7 +1403,7 @@ namespace SqlSugar if (this.IsMasterSlaveSeparation && IsRead(sql) && this.Transaction == null) { this.Connection = this.MasterConnection; - this.Context.CurrentConnectionConfig.ConnectionString = this.MasterConnection.ConnectionString; + this.Context.CurrentConnectionConfig.ConnectionString = this.MasterConnectionString; } }