diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs index f42c7de56..d9d00769d 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs @@ -84,7 +84,46 @@ namespace OrmTest Console.Write(item.StorageMessage+" "); } db.DbMaintenance.TruncateTable(); + IDemo2(); + } + + private static void IDemo1() + { + var db = Db; + db.DbMaintenance.TruncateTable(); + List list2 = new List(); + list2.Add(new UinitBlukTable() { Id = 1, Name = "a", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 2, Name = "a", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 0, Name = "a", Create = DateTime.Now }); + + var x = Db.Storageable(list2) + .SplitUpdate(it => it.Item.Id > 0) + .SplitInsert(it => it.Item.Id == 0).ToStorage(); + x.AsInsertable.ExecuteCommand(); + x.AsUpdateable.ExecuteCommand(); + + + db.DbMaintenance.TruncateTable(); + } + private static void IDemo2() + { + var db = Db; + db.DbMaintenance.TruncateTable(); + List list2 = new List(); + list2.Add(new UinitBlukTable() { Id = 1, Name = "a1", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 2, Name = "a2", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 3, Name = "a3", Create = DateTime.Now }); + db.Insertable(list2[1]).ExecuteCommand(); + + var x = Db.Storageable(list2) + .SplitUpdate(it => it.Any(y=>y.Id==it.Item.Id)) + .SplitInsert(it => it.NotAny(y => y.Id == it.Item.Id)).ToStorage(); + x.AsInsertable.ExecuteCommand(); + x.AsUpdateable.ExecuteCommand(); + db.DbMaintenance.TruncateTable(); + } + public class UinitBlukTable { [SqlSugar.SugarColumn(IsPrimaryKey =true)] diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 4129622d6..b0f3fd34e 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -197,6 +197,70 @@ namespace SqlSugar { return WhereClass(new List() { whereClass }, ignoreDefaultValue); } + public ISugarQueryable WhereClassByPrimaryKey(List list) + { + _WhereClassByPrimaryKey(list); + return this; + } + public ISugarQueryable WhereClassByPrimaryKey(T data) + { + _WhereClassByPrimaryKey(new List() { data }); + return this; + } + + /// + /// if a property that is primary key is a condition + /// + /// + /// + public ISugarQueryable _WhereClassByPrimaryKey(List whereClassTypes) + { + + if (whereClassTypes.HasValue()) + { + var columns = this.Context.EntityMaintenance.GetEntityInfo().Columns.Where(it => it.IsIgnore == false && it.IsPrimarykey == true).ToList(); + Check.Exception(columns == null || columns.Count == 0, "{0} no primary key, Can not use WhereClassByPrimaryKey ", typeof(T).Name); + Check.Exception(this.QueryBuilder.IsSingle() == false, "No support join query"); + List whereModels = new List(); + foreach (var item in whereClassTypes) + { + var cons = new ConditionalCollections(); + foreach (var column in columns) + { + WhereType WhereType = WhereType.And; + var value = column.PropertyInfo.GetValue(item, null); + if (cons.ConditionalList == null) + { + cons.ConditionalList = new List>(); + if (QueryBuilder.WhereInfos.IsNullOrEmpty() && whereModels.IsNullOrEmpty()) + { + + } + else + { + WhereType = WhereType.Or; + } + } + cons.ConditionalList.Add(new KeyValuePair(WhereType, new ConditionalModel() + { + ConditionalType = ConditionalType.Equal, + FieldName = this.QueryBuilder.Builder.GetTranslationColumnName(column.DbColumnName), + FieldValue = value.ObjToString() + })); + } + if (cons.HasValue()) + { + whereModels.Add(cons); + } + } + this.Where(whereModels); + } + else + { + this.Where(" 1=2 "); + } + return this; + } /// /// if a property that is not empty is a condition /// diff --git a/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs b/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs index 35d4245a8..415203ed9 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs @@ -71,7 +71,10 @@ namespace SqlSugar } if (whereExpression == null && pkInfos.Any()) { - + this.Context.Utilities.PageEach(allDatas, 300, item => { + var addItems=this.Context.Queryable().WhereClassByPrimaryKey(item.Select(it => it.Item).ToList()).ToList(); + dbDataList.AddRange(addItems); + }); } var messageList = allDatas.Select(it => new StorageableMessage() { diff --git a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs index e522f4dcb..5340e228b 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs @@ -41,8 +41,9 @@ namespace SqlSugar /// /// /// - ISugarQueryable WhereClass(List whereClassList,bool ignoreDefaultValue = false) where ClassType : class, new(); - + ISugarQueryable WhereClass(List whereClassList,bool ignoreDefaultValue = false) where ClassType : class, new(); + ISugarQueryable WhereClassByPrimaryKey(List list); + ISugarQueryable WhereClassByPrimaryKey(T data) ; ISugarQueryable Where(Expression> expression); ISugarQueryable Where(string whereString, object parameters = null); ISugarQueryable Where(List conditionalModels);