From 5e6b615c3003b5b40bfcc492784fd635aebce69f Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Fri, 10 Nov 2023 18:05:13 +0800 Subject: [PATCH] =?UTF-8?q?v1.6.7=20=E5=A2=9E=E5=8A=A0=20using=20(fsql.Cha?= =?UTF-8?q?nge())=20=E7=94=A8=E5=AE=8C=E5=88=87=E5=9B=9E=E6=9D=A5=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++++ src/FreeSql.Cloud/FreeSql.Cloud.csproj | 2 +- src/FreeSql.Cloud/FreeSqlCloud.cs | 4 ++-- src/FreeSql.Cloud/FreeSqlCloundSnapshot.cs | 14 ++++++++++++-- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e68bc78..e19be14 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,11 @@ fsql.Change(DbEnum.db2).Select(); fsql.Use(DbEnum.db2).Select(); //单次有效 + +using (fsql.Change(DbEnum.db2)) { + //todo.. +} +//FreeSql.Cloud v1.6.7 一个范围内切换,之后再切换回去 ``` 自动定向数据库配置: diff --git a/src/FreeSql.Cloud/FreeSql.Cloud.csproj b/src/FreeSql.Cloud/FreeSql.Cloud.csproj index 83cca19..90db61b 100644 --- a/src/FreeSql.Cloud/FreeSql.Cloud.csproj +++ b/src/FreeSql.Cloud/FreeSql.Cloud.csproj @@ -2,7 +2,7 @@ netstandard20;net40 - 1.6.6 + 1.6.7 true FreeSql;ncc;YeXiangQin 提供跨数据库访问,分布式事务TCC、SAGA解决方案,支持 .NET Core 2.1+, .NET Framework 4.0+. diff --git a/src/FreeSql.Cloud/FreeSqlCloud.cs b/src/FreeSql.Cloud/FreeSqlCloud.cs index 974b71f..f06e701 100644 --- a/src/FreeSql.Cloud/FreeSqlCloud.cs +++ b/src/FreeSql.Cloud/FreeSqlCloud.cs @@ -89,7 +89,7 @@ public IFreeSql Change(TDBKey dbkey) var oldkey = _dbkeyCurrent.Value; if (_distributeTraceEnable && object.Equals(dbkey, oldkey) == false) _distributedTraceCall($"数据库切换[Change] {oldkey} -> {dbkey}"); _dbkeyCurrent.Value = dbkey; - return new FreeSqlCloundSnapshot(this, dbkey); + return new FreeSqlCloundSnapshot(this, dbkey, oldkey); } /// /// 临时使用数据库(单次有效) @@ -100,7 +100,7 @@ public IFreeSql Use(TDBKey dbkey) { var oldkey = _dbkeyCurrent.Value; if (_distributeTraceEnable && object.Equals(dbkey, oldkey) == false) _distributedTraceCall($"数据库使用[Use] {dbkey}"); - return new FreeSqlCloundSnapshot(this, dbkey); + return new FreeSqlCloundSnapshot(this, dbkey, default); } internal IFreeSql GetBySnapshot(TDBKey dbkey) { diff --git a/src/FreeSql.Cloud/FreeSqlCloundSnapshot.cs b/src/FreeSql.Cloud/FreeSqlCloundSnapshot.cs index 4cadc46..603637a 100644 --- a/src/FreeSql.Cloud/FreeSqlCloundSnapshot.cs +++ b/src/FreeSql.Cloud/FreeSqlCloundSnapshot.cs @@ -13,11 +13,13 @@ class FreeSqlCloundSnapshot : IFreeSql { readonly FreeSqlCloud _fsqlc; readonly TDBKey _current; + readonly TDBKey _old; - public FreeSqlCloundSnapshot(FreeSqlCloud fsqlc, TDBKey current) + public FreeSqlCloundSnapshot(FreeSqlCloud fsqlc, TDBKey current, TDBKey old) { _fsqlc = fsqlc; _current = current; + _old = old; } public IAdo Ado => _fsqlc.GetBySnapshot(_current).Ado; @@ -25,7 +27,15 @@ public FreeSqlCloundSnapshot(FreeSqlCloud fsqlc, TDBKey current) public ICodeFirst CodeFirst => _fsqlc.GetBySnapshot(_current).CodeFirst; public IDbFirst DbFirst => _fsqlc.GetBySnapshot(_current).DbFirst; public GlobalFilter GlobalFilter => _fsqlc.GetBySnapshot(_current).GlobalFilter; - public void Dispose() { } + public void Dispose() + { + //示例 + //using (_fsqlc.Change("db2")) + //{ + + //} + _fsqlc.Change(_old); + } public void Transaction(Action handler) => _fsqlc.GetBySnapshot(_current).Transaction(handler); public void Transaction(IsolationLevel isolationLevel, Action handler) => _fsqlc.GetBySnapshot(_current).Transaction(isolationLevel, handler);