From b3f7fc29d138b5305ca98ea61b6237609c38f5f2 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Fri, 25 Jan 2019 21:45:15 +0800 Subject: [PATCH] Debugging has perfect functions --- ...IInsertOrUpdate.cs => I_InsertOrUpdate.cs} | 2 +- Src/Asp.Net/SqlServerTest/Demos/J_Debugger.cs | 51 +++++++++++++++++++ Src/Asp.Net/SqlServerTest/Program.cs | 3 +- .../SqlServerTest/SqlServerTest.csproj | 3 +- .../Abstract/AdoProvider/AdoProvider.cs | 16 +++--- .../DeleteProvider/DeleteableProvider.cs | 1 + .../InsertableProvider/InsertableProvider.cs | 2 +- .../QueryableProvider/QueryableProvider.cs | 1 + .../UpdateProvider/UpdateableProvider.cs | 1 + .../Infrastructure/SqlSugarAccessory.cs | 1 + 10 files changed, 69 insertions(+), 12 deletions(-) rename Src/Asp.Net/SqlServerTest/Demos/{IInsertOrUpdate.cs => I_InsertOrUpdate.cs} (97%) create mode 100644 Src/Asp.Net/SqlServerTest/Demos/J_Debugger.cs diff --git a/Src/Asp.Net/SqlServerTest/Demos/IInsertOrUpdate.cs b/Src/Asp.Net/SqlServerTest/Demos/I_InsertOrUpdate.cs similarity index 97% rename from Src/Asp.Net/SqlServerTest/Demos/IInsertOrUpdate.cs rename to Src/Asp.Net/SqlServerTest/Demos/I_InsertOrUpdate.cs index 44aa0ff5c..d026d88b1 100644 --- a/Src/Asp.Net/SqlServerTest/Demos/IInsertOrUpdate.cs +++ b/Src/Asp.Net/SqlServerTest/Demos/I_InsertOrUpdate.cs @@ -7,7 +7,7 @@ using System.Text; namespace OrmTest.Demo { - public class IInsertOrUpdate : DemoBase + public class InsertOrUpdate : DemoBase { public static void Init() { diff --git a/Src/Asp.Net/SqlServerTest/Demos/J_Debugger.cs b/Src/Asp.Net/SqlServerTest/Demos/J_Debugger.cs new file mode 100644 index 000000000..0b9682f07 --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/Demos/J_Debugger.cs @@ -0,0 +1,51 @@ +using OrmTest.Demo; +using OrmTest.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest.Demo +{ + public class Debugger : DemoBase + { + public static void Init() + { + var db = GetInstance(); + db.CurrentConnectionConfig.Debugger = new SqlSugar.SugarDebugger() { EnableThreadSecurityValidation = true }; + + db.Queryable().ToList(); + db.Queryable().ToListAsync().Wait(); + db.Insertable(new Student() { Name = "a" }).ExecuteCommandAsync(); + db.Updateable(new Student() { Name = "a" }).ExecuteCommandAsync(); + db.Deleteable(1111).ExecuteCommandAsync(); + try + { + var task = new Task(() => + { + + try + { + //error + db.Queryable().ToList(); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + + } + + }); + task.Start(); + task.Wait(); + } + finally + { + + } + } + + + } +} \ No newline at end of file diff --git a/Src/Asp.Net/SqlServerTest/Program.cs b/Src/Asp.Net/SqlServerTest/Program.cs index 954190e86..73907dc3c 100644 --- a/Src/Asp.Net/SqlServerTest/Program.cs +++ b/Src/Asp.Net/SqlServerTest/Program.cs @@ -60,7 +60,8 @@ namespace OrmTest Demo.ExtEntity.Init(); Demo.VersionValidation.Init(); Demo.Delete.Init(); - Demo.IInsertOrUpdate.Init(); + Demo.InsertOrUpdate.Init(); + Demo.Debugger.Init(); } } } diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj index ab7e5296c..b30d5d9ae 100644 --- a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj +++ b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj @@ -58,7 +58,8 @@ - + + diff --git a/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs index 3ba8da5a3..213f785bc 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs @@ -675,19 +675,19 @@ namespace SqlSugar } public virtual void ExecuteBefore(string sql, SugarParameter[] parameters) { - if (this.Context.CurrentConnectionConfig.Debugger != null && this.Context.CurrentConnectionConfig.Debugger.EnableThreadSecurityValidation == true) { + if (this.Context.IsAsyncMethod==false&&this.Context.CurrentConnectionConfig.Debugger != null && this.Context.CurrentConnectionConfig.Debugger.EnableThreadSecurityValidation == true) { - var processId = Process.GetCurrentProcess().Id; - var contextId = this.Context.ContextID.ToString(); + var contextId =this.Context.ContextID.ToString(); + var processId = Process.GetCurrentProcess().Id.ToString(); var cache = new ReflectionInoCacheService(); - if (!cache.ContainsKey(contextId)) + if (!cache.ContainsKey(processId)) { - cache.Add(contextId, processId); + cache.Add(processId, contextId); } else { - var cacheValue = cache.Get(contextId); - if (processId != cacheValue) { - new SqlSugarException(this.Context,ErrorMessage.GetThrowMessage("Detection of SqlSugarClient cross-threading usage,a thread needs a new one", "检测到声名的SqlSugarClient跨线程使用,请检查是否静态、是否单例、或者IOC配置错误引起的,保证一个线程new出一个对象 ,具本Sql:")+sql,parameters); + var cacheValue = cache.Get(processId); + if (contextId != cacheValue) { + throw new SqlSugarException(this.Context,ErrorMessage.GetThrowMessage("Detection of SqlSugarClient cross-threading usage,a thread needs a new one", "检测到声名的SqlSugarClient跨线程使用,请检查是否静态、是否单例、或者IOC配置错误引起的,保证一个线程new出一个对象 ,具本Sql:")+sql,parameters); } } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs index 6eeb7df85..17aa9764f 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs @@ -378,6 +378,7 @@ namespace SqlSugar { var asyncContext = this.Context.Utilities.CopyContext(true); asyncContext.CurrentConnectionConfig.IsAutoCloseConnection = true; + asyncContext.IsAsyncMethod = true; var asyncDeleteable = asyncContext.Deleteable(); var asyncDeleteBuilder = asyncDeleteable.DeleteBuilder; diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index 013d6ffdf..04fccba97 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -425,7 +425,7 @@ namespace SqlSugar { var asyncContext = this.Context.Utilities.CopyContext(true); asyncContext.CurrentConnectionConfig.IsAutoCloseConnection = true; - + asyncContext.IsAsyncMethod = true; var asyncInsertable = asyncContext.Insertable(this.InsertObjs); var asyncInsertableBuilder = asyncInsertable.InsertBuilder; asyncInsertableBuilder.DbColumnInfoList = this.InsertBuilder.DbColumnInfoList; diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 345d98ed9..725b68fd5 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -1557,6 +1557,7 @@ namespace SqlSugar protected ISugarQueryable CopyQueryable() { var asyncContext = this.Context.Utilities.CopyContext(true); + asyncContext.IsAsyncMethod = true; asyncContext.CurrentConnectionConfig.IsAutoCloseConnection = true; var asyncQueryable = asyncContext.Queryable().Select(string.Empty).WithCacheIF(IsCache, CacheTime); if (this.MapperAction != null) diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index 52d1e86b1..99a69a01d 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -526,6 +526,7 @@ namespace SqlSugar { var asyncContext = this.Context.Utilities.CopyContext(true); asyncContext.CurrentConnectionConfig.IsAutoCloseConnection = true; + asyncContext.IsAsyncMethod = true; var asyncUpdateable = asyncContext.Updateable(this.UpdateObjs); var asyncUpdateableBuilder = asyncUpdateable.UpdateBuilder; diff --git a/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs b/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs index 931db407b..25a9811ef 100644 --- a/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs +++ b/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs @@ -46,6 +46,7 @@ namespace SqlSugar public Dictionary TempItems { get; set; } public bool IsSystemTablesConfig { get { return this.CurrentConnectionConfig.InitKeyType == InitKeyType.SystemTable; } } public Guid ContextID { get; set; } + internal bool IsAsyncMethod { get; set; } public MappingTableList MappingTables = new MappingTableList(); public MappingColumnList MappingColumns = new MappingColumnList(); public IgnoreColumnList IgnoreColumns = new IgnoreColumnList();