diff --git a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs index 87ef3ce4d..be940c16c 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs @@ -956,6 +956,11 @@ namespace SqlSugar #endregion #region SimpleClient + public SugarUnitOfWork CreateContext(bool isTran = true) + { + Check.ExceptionEasy(" var childDb=Db.GetConnection(configId); use Db.CreateContext ", " 例如 var childDb=Db.GetConnection(configId);其中Db才能使用CreateContext,childDb不能使用"); + return null; + } //[Obsolete("Use SqlSugarClient.GetSimpleClient() Or SqlSugarClient.GetSimpleClient() ")] //public virtual SimpleClient SimpleClient //{ diff --git a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs index 42de4e6e8..edc7e0f33 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs @@ -188,7 +188,11 @@ namespace SqlSugar { return ScopedContext.GetDate(); } - + public SugarUnitOfWork CreateContext(bool isTran = true) + { + Check.ExceptionEasy(" var childDb=Db.GetConnection(configId); use Db.CreateContext ", " 例如 var childDb=Db.GetConnection(configId);其中Db才能使用CreateContext,childDb不能使用"); + return null; + } public SimpleClient GetSimpleClient() where T : class, new() { return ScopedContext.GetSimpleClient(); diff --git a/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs b/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs index 15165f4b5..3abdfd7e2 100644 --- a/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs +++ b/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs @@ -44,6 +44,7 @@ namespace SqlSugar #endregion #region Other methods + SugarUnitOfWork CreateContext(bool isTran = true); SplitTableContext SplitHelper() where T : class, new(); SplitTableContextResult SplitHelper(T data) where T : class, new(); SplitTableContextResult SplitHelper(List data) where T : class, new(); diff --git a/Src/Asp.Net/SqlSugar/SimpleClient.cs b/Src/Asp.Net/SqlSugar/SimpleClient.cs index e4ba9daa0..758226072 100644 --- a/Src/Asp.Net/SqlSugar/SimpleClient.cs +++ b/Src/Asp.Net/SqlSugar/SimpleClient.cs @@ -23,7 +23,7 @@ namespace SqlSugar return this.Context; } - private SimpleClient() + public SimpleClient() { } diff --git a/Src/Asp.Net/SqlSugar/SqlSugar.csproj b/Src/Asp.Net/SqlSugar/SqlSugar.csproj index 6073863bf..f38b251e2 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugar.csproj +++ b/Src/Asp.Net/SqlSugar/SqlSugar.csproj @@ -103,6 +103,7 @@ + diff --git a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs index 3a8886064..f872f8f07 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs @@ -67,6 +67,19 @@ namespace SqlSugar #endregion #region SimpleClient + public SugarUnitOfWork CreateContext(bool isTran = true) + { + SugarUnitOfWork sugarUnitOf = new SugarUnitOfWork(); + sugarUnitOf.Db = this; + sugarUnitOf.Tenant = this; + sugarUnitOf.IsTran = true; + this.Open(); + if (isTran) + { + this.BeginTran(); + } + return sugarUnitOf; + } public SimpleClient GetSimpleClient() where T : class, new() { return this.Context.GetSimpleClient(); diff --git a/Src/Asp.Net/SqlSugar/SqlSugarScope.cs b/Src/Asp.Net/SqlSugar/SqlSugarScope.cs index 62f6d527f..028262a64 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugarScope.cs +++ b/Src/Asp.Net/SqlSugar/SqlSugarScope.cs @@ -175,6 +175,11 @@ namespace SqlSugar return ScopedContext.GetDate(); } + public SugarUnitOfWork CreateContext(bool isTran = true) + { + return ScopedContext.CreateContext(isTran); + } + public SimpleClient GetSimpleClient() where T : class, new() { return ScopedContext.GetSimpleClient(); diff --git a/Src/Asp.Net/SqlSugar/SugarUnitOfWork.cs b/Src/Asp.Net/SqlSugar/SugarUnitOfWork.cs new file mode 100644 index 000000000..c14618f1a --- /dev/null +++ b/Src/Asp.Net/SqlSugar/SugarUnitOfWork.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public class SugarUnitOfWork : IDisposable + { + public ISqlSugarClient Db { get; internal set; } + public ITenant Tenant { get; internal set; } + public bool IsTran { get; internal set; } + public bool IsCommit { get; internal set; } + public bool IsClose { get; internal set; } + + public void Dispose() + { + + if (this.IsTran && IsCommit == false) + { + this.Tenant.RollbackTran(); + } + if (IsClose == false) + { + this.Db.Close(); + } + } + + public SimpleClient GetRepository() where T : class, new() + { + return new SimpleClient(Db); + } + + public RepositoryType GetMyRepository() where RepositoryType : ISugarRepository, new() + { + var result = new RepositoryType(); + result.Context = this.Db; + return result; + } + + public void Commit() + { + if (this.IsTran && this.IsCommit == false) + { + this.Tenant.CommitTran(); + IsCommit = true; + } + if (this.IsClose == false) + { + this.Db.Close(); + IsClose = true; + } + } + } +}