From 37b1464280800c8c90c4c9d3babadc5f927db544 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Wed, 19 Jul 2023 23:41:42 +0800 Subject: [PATCH] Synchronization code --- .../QueryableProvider/QueryableProvider.cs | 6 ++++-- .../Abstract/SugarProvider/SqlSugarProvider.cs | 1 + .../SqlSugar/Utilities/UtilMethods.cs | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 397826948..5bb155719 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -379,16 +379,18 @@ namespace SqlSugar this.ClearFilter(typeof(FilterType1), typeof(FilterType2),typeof(FilterType3)); return this; } - public ISugarQueryable Filter(Type type) + public ISugarQueryable Filter(Type type) { this.Context.InitMappingInfo(type); var whereString= QueryBuilder.GetFilters(type); - if (whereString.HasValue()) + if (whereString.HasValue()) { this.Where(whereString); } + UtilMethods.AddDiscrimator(type,this); return this; } + public virtual ISugarQueryable Mapper(Action mapperAction) { this.MapperAction=UtilMethods.IsNullReturnNew(this.MapperAction); diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs index 668f0647a..44fcd214b 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs @@ -127,6 +127,7 @@ namespace SqlSugar InitMappingInfo(); var result = this.CreateQueryable(); + UtilMethods.AddDiscrimator(typeof(T), result); return result; } /// diff --git a/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs b/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs index 03e702399..fb7320fab 100644 --- a/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs +++ b/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs @@ -1265,5 +1265,23 @@ namespace SqlSugar var method = value.GetType().GetMethods().First(it => it.GetParameters().Length == 0 && it.Name == "ToShortDateString"); return method.Invoke(value, new object[] { }); } + + + internal static void AddDiscrimator(Type type, ISugarQueryable queryable) + { + var entityInfo = queryable.Context?.EntityMaintenance?.GetEntityInfoWithAttr(type); + if (entityInfo!=null&&entityInfo.Discrimator.HasValue()) + { + Check.ExceptionEasy(!Regex.IsMatch(entityInfo.Discrimator, @"^(?:\w+:\w+)(?:,\w+:\w+)*$"), "The format should be type:cat for this type, and if there are multiple, it can be FieldName:cat,FieldName2:dog ", "格式错误应该是type:cat这种格式,如果是多个可以FieldName:cat,FieldName2:dog,不要有空格"); + var array = entityInfo.Discrimator.Split(','); + foreach (var disItem in array) + { + var name = disItem.Split(':').First(); + var value = disItem.Split(':').Last(); + queryable.Where(name, "=", value); + } + } + } + } }