diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs index 6e19fa44d..1d2d6aa36 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs @@ -89,7 +89,94 @@ namespace OrmTest IDemo2(); IDemo3(); IDemo4(); + IDemo5(); + IDemo6(); } + + private static void IDemo6() + { + var db = Db; + db.DbMaintenance.TruncateTable(); + + List list2 = new List(); + list2.Add(new UinitBlukTable() { Id = 1, Name = "a" }); + list2.Add(new UinitBlukTable() { Id = 2, Name = "a" }); + list2.Add(new UinitBlukTable() { Id = 3, Name = "a" }); + list2.Add(new UinitBlukTable() { Id = 4, Name = "" }); + + db.Insertable(list2.First()).ExecuteCommand();//插入第一条 + + var x = Db.Storageable(list2) + .SplitUpdate(it => it.Any()) + .SplitInsert(it => true) + .ToStorage(); + + //var x2 = Db.Storageable(list2) + // .Saveable() + // .ToStorage(); + Console.WriteLine(" 插入 {0} 更新{1} 错误数据{2} 不计算数据{3} 删除数据{4},总共{5}", + x.InsertList.Count, + x.UpdateList.Count, + x.ErrorList.Count, + x.IgnoreList.Count, + x.DeleteList.Count, + x.TotalList.Count + ); + foreach (var item in x.ErrorList) + { + Console.WriteLine("id等于" + item.Item.Id + " : " + item.StorageMessage); + } + + int i = x.AsInsertable.ExecuteCommand(); + Console.WriteLine(i + "条成功插入"); + i = x.AsUpdateable.ExecuteCommand(); + Console.WriteLine(i + "条成功更新"); + + db.DbMaintenance.TruncateTable(); + } + private static void IDemo5() + { + var db = Db; + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + + List list2 = new List(); + list2.Add(new UinitBlukTable3() { Id = 1, Name = "a" }); + list2.Add(new UinitBlukTable3() { Id = 2, Name = "a" }); + list2.Add(new UinitBlukTable3() { Id = 3, Name = "a" }); + list2.Add(new UinitBlukTable3() { Id = 4, Name = "" }); + + db.Insertable(list2.First()).ExecuteCommand();//插入第一条 + + var x = Db.Storageable(list2) + .SplitUpdate(it=>it.Any()) + .SplitInsert(it => true) + .ToStorage(); + + //var x2 = Db.Storageable(list2) + // .Saveable() + // .ToStorage(); + Console.WriteLine(" 插入 {0} 更新{1} 错误数据{2} 不计算数据{3} 删除数据{4},总共{5}", + x.InsertList.Count, + x.UpdateList.Count, + x.ErrorList.Count, + x.IgnoreList.Count, + x.DeleteList.Count, + x.TotalList.Count + ); + foreach (var item in x.ErrorList) + { + Console.WriteLine("id等于" + item.Item.Id + " : " + item.StorageMessage); + } + + int i = x.AsInsertable.ExecuteCommand(); + Console.WriteLine(i + "条成功插入"); + i = x.AsUpdateable.ExecuteCommand(); + Console.WriteLine(i + "条成功更新"); + + db.DbMaintenance.TruncateTable(); + } + private static void IDemo4() { var db = Db; @@ -228,7 +315,20 @@ namespace OrmTest { public string Name { get; set; } public int Id { get; set; } + [SqlSugar.SugarColumn(IsNullable = true)] + public DateTime? Create { get; set; } } - + + [SqlSugar.SugarTable("UinitBlukTable4")] + public class UinitBlukTable3 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true)] + public string Name { get; set; } + [SqlSugar.SugarColumn(IsPrimaryKey = true)] + public int Id { get; set; } + [SqlSugar.SugarColumn(IsNullable = true)] + public DateTime? Create { get; set; } + } + } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs b/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs index 1618c3a9f..81bf158eb 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs @@ -42,6 +42,13 @@ namespace SqlSugar whereFuncs.Add(new KeyValuePair, bool>, string>(StorageType.Update, conditions, message)); return this; } + + public IStorageable Saveable(string inserMessage = null,string updateMessage=null) + { + return this + .SplitUpdate(it => it.Any(),updateMessage) + .SplitInsert(it => true, inserMessage); + } public IStorageable SplitError(Func, bool> conditions, string message = null) { whereFuncs.Add(new KeyValuePair, bool>, string>(StorageType.Error, conditions, message)); @@ -62,7 +69,6 @@ namespace SqlSugar public StorageableResult ToStorage() { - var entity = this.Context.EntityMaintenance.GetEntityInfo(); if (this.allDatas.Count == 0) return new StorageableResult(); var pkInfos = this.Context.EntityMaintenance.GetEntityInfo().Columns.Where(it => it.IsPrimarykey); @@ -77,7 +83,7 @@ namespace SqlSugar dbDataList.AddRange(addItems); }); } - var pkProperties = GetPkProperties(pkInfos, entity); + var pkProperties = GetPkProperties(pkInfos); var messageList = allDatas.Select(it => new StorageableMessage() { Item = it.Item, @@ -123,12 +129,18 @@ namespace SqlSugar return result; } - private string[] GetPkProperties(IEnumerable pkInfos,EntityInfo entity) + private string[] GetPkProperties(IEnumerable pkInfos) { - return entity.Columns - .Where(it => it.IsPrimarykey||pkInfos.Any(y=>y.PropertyName==it.PropertyName)).Select(it => it.PropertyName).ToArray(); + if (whereExpression == null) + { + return pkInfos.Select(it => it.PropertyName).ToArray(); + } + else + { + return wherecolumnList.Select(it => it.PropertyName).ToArray(); + } } - + List wherecolumnList; public IStorageable WhereColumns(Expression> columns) { if (columns == null) @@ -141,6 +153,7 @@ namespace SqlSugar it.DbColumnName.Equals(y, StringComparison.CurrentCultureIgnoreCase) || it.PropertyName.Equals(y, StringComparison.CurrentCultureIgnoreCase)) ).ToList(); + wherecolumnList = whereColumns; if (whereColumns.Count == 0) { whereColumns = dbColumns.Where(it => it.IsPrimarykey).ToList(); diff --git a/Src/Asp.Net/SqlSugar/Interface/IStorageable.cs b/Src/Asp.Net/SqlSugar/Interface/IStorageable.cs index bea168f45..5cf690099 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IStorageable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IStorageable.cs @@ -11,6 +11,7 @@ namespace SqlSugar IStorageable WhereColumns(Expression> columns); IStorageable SplitInsert(Func, bool> conditions, string message=null); IStorageable SplitUpdate(Func, bool> conditions, string message = null); + IStorageable Saveable(string inserMessage = null, string updateMessage = null); IStorageable SplitError(Func, bool> conditions, string message = null); IStorageable SplitIgnore(Func, bool> conditions, string message = null); IStorageable SplitDelete(Func, bool> conditions, string message = null);