diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs index e4dc4ca71..f42c7de56 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs @@ -64,28 +64,30 @@ namespace OrmTest } list2.First().Name = null; db.DbMaintenance.TruncateTable(); + db.Insertable(new UinitBlukTable() { Id = 2, Name = "b", Create = DateTime.Now }).ExecuteCommand(); var x=Db.Storageable(list2) - .SplitInsert(it => !string.IsNullOrEmpty(it.Item.Name)) - .SplitUpdate(it =>it.Database.Any(y=>y.Id==it.Item.Id)) + .SplitInsert(it => it.NotAny(y=>y.Id==it.Item.Id)) + .SplitUpdate(it => it.Any(y => y.Id == it.Item.Id)) .SplitDelete(it=>it.Item.Id>10) - .SplitIgnore(it=>it.Item.Id==2) + .SplitIgnore(it=>it.Item.Id==1) .SplitError(it => it.Item.Id == 3,"id不能等于3") .SplitError(it => it.Item.Id == 4, "id不能等于4") .SplitError(it => it.Item.Id == 5, "id不能等于5") - .WhereColumns(it=>it.Id) + .SplitError(it => it.Item.Name==null, "name不能等于") + .WhereColumns(it=> new { it.Id }) .ToStorage(); x.AsDeleteable.ExecuteCommand(); x.AsInsertable.ExecuteCommand(); x.AsUpdateable.ExecuteCommand(); foreach (var item in x.ErrorList) { - Console.Write(item.StorageMessage); + Console.Write(item.StorageMessage+" "); } db.DbMaintenance.TruncateTable(); } public class UinitBlukTable { - + [SqlSugar.SugarColumn(IsPrimaryKey =true)] public int Id { get; set; } public string Name { get; set; } [SqlSugar.SugarColumn(IsNullable =true)] diff --git a/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs b/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs index 25e5fafb8..35d4245a8 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs @@ -15,7 +15,7 @@ namespace SqlSugar List> allDatas = new List>(); List dbDataList = new List(); List, bool>, string>> whereFuncs = new List, bool>, string>>(); - Expression> columns; + Expression> whereExpression; public Storageable(List datas, SqlSugarProvider context) { this.Context = context; @@ -64,6 +64,15 @@ namespace SqlSugar { if (this.allDatas.Count == 0) return new StorageableResult(); + var pkInfos = this.Context.EntityMaintenance.GetEntityInfo().Columns.Where(it => it.IsPrimarykey); + if (whereExpression==null&&!pkInfos.Any()) + { + Check.Exception(true, "Need primary key or WhereColumn"); + } + if (whereExpression == null && pkInfos.Any()) + { + + } var messageList = allDatas.Select(it => new StorageableMessage() { Item = it.Item, @@ -99,10 +108,10 @@ namespace SqlSugar IgnoreList = ignore, TotalList = messageList }; - if (this.columns != null) + if (this.whereExpression != null) { - result.AsUpdateable.WhereColumns(columns); - result.AsDeleteable.WhereColumns(columns); + result.AsUpdateable.WhereColumns(whereExpression); + result.AsDeleteable.WhereColumns(whereExpression); } result.AsDeleteable.Where(delete.Select(it => it.Item).ToList()); return result; @@ -120,9 +129,13 @@ namespace SqlSugar it.DbColumnName.Equals(y, StringComparison.CurrentCultureIgnoreCase) || it.PropertyName.Equals(y, StringComparison.CurrentCultureIgnoreCase)) ).ToList(); + if (whereColumns.Count == 0) + { + whereColumns = dbColumns.Where(it => it.IsPrimarykey).ToList(); + } if (whereColumns.Count > 0) { - this.Context.Utilities.PageEach(allDatas, 300, itemList => + this.Context.Utilities.PageEach(allDatas, 200, itemList => { List conditList = new List(); SetConditList(itemList, whereColumns, conditList); @@ -130,27 +143,31 @@ namespace SqlSugar this.dbDataList.AddRange(addItem); }); } + this.whereExpression = columns; return this; } } private static void SetConditList(List> itemList, List whereColumns, List conditList) { - var condition = new ConditionalCollections() - { - ConditionalList = new List>() - }; + ; foreach (var dataItem in itemList) { + var condition = new ConditionalCollections() + { + ConditionalList = new List>() + }; + conditList.Add(condition); + int i = 0; foreach (var item in whereColumns) { - conditList.Add(condition); - condition.ConditionalList.Add(new KeyValuePair(WhereType.And, new ConditionalModel() + condition.ConditionalList.Add(new KeyValuePair(i==0?WhereType.Or :WhereType.And, new ConditionalModel() { FieldName = item.DbColumnName, ConditionalType = ConditionalType.Equal, - FieldValue = item.PropertyInfo.GetValue(dataItem, null) + "" + FieldValue = item.PropertyInfo.GetValue(dataItem.Item, null) + "" })); + ++i; } } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs index d2b32a4e0..e2e53c483 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs @@ -742,8 +742,9 @@ namespace SqlSugar public IStorageable Storageable(List dataList) where T : class, new() { this.InitMappingInfo(); + var sqlBuilder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig); var result= new Storageable(dataList,this); - result.Builder = this._SqlBuilder; + result.Builder = sqlBuilder; return result; } #endregion diff --git a/Src/Asp.Net/SqlSugar/Interface/IStorageable.cs b/Src/Asp.Net/SqlSugar/Interface/IStorageable.cs index 83866ad2f..cced69317 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IStorageable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IStorageable.cs @@ -21,7 +21,7 @@ namespace SqlSugar public class StorageableInfo where T : class, new() { public T Item { get; set; } - public List Database { get; set; } + internal List Database { get; set; } public bool Any(Func expression) { return Database.Any(expression);