Update db.Storageable

This commit is contained in:
skx 2021-02-19 22:30:44 +08:00
parent 99d64a9ea7
commit fb1235858f
3 changed files with 121 additions and 7 deletions

View File

@ -89,7 +89,94 @@ namespace OrmTest
IDemo2();
IDemo3();
IDemo4();
IDemo5();
IDemo6();
}
private static void IDemo6()
{
var db = Db;
db.DbMaintenance.TruncateTable<UinitBlukTable>();
List<UinitBlukTable> list2 = new List<UinitBlukTable>();
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<UinitBlukTable>();
}
private static void IDemo5()
{
var db = Db;
db.CodeFirst.InitTables<UinitBlukTable3>();
db.DbMaintenance.TruncateTable<UinitBlukTable3>();
List<UinitBlukTable3> list2 = new List<UinitBlukTable3>();
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<UinitBlukTable>();
}
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; }
}
}
}

View File

@ -42,6 +42,13 @@ namespace SqlSugar
whereFuncs.Add(new KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>(StorageType.Update, conditions, message));
return this;
}
public IStorageable<T> Saveable(string inserMessage = null,string updateMessage=null)
{
return this
.SplitUpdate(it => it.Any(),updateMessage)
.SplitInsert(it => true, inserMessage);
}
public IStorageable<T> SplitError(Func<StorageableInfo<T>, bool> conditions, string message = null)
{
whereFuncs.Add(new KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>(StorageType.Error, conditions, message));
@ -62,7 +69,6 @@ namespace SqlSugar
public StorageableResult<T> ToStorage()
{
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
if (this.allDatas.Count == 0)
return new StorageableResult<T>();
var pkInfos = this.Context.EntityMaintenance.GetEntityInfo<T>().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<T>()
{
Item = it.Item,
@ -123,12 +129,18 @@ namespace SqlSugar
return result;
}
private string[] GetPkProperties(IEnumerable<EntityColumnInfo> pkInfos,EntityInfo entity)
private string[] GetPkProperties(IEnumerable<EntityColumnInfo> 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<EntityColumnInfo> wherecolumnList;
public IStorageable<T> WhereColumns(Expression<Func<T, object>> 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();

View File

@ -11,6 +11,7 @@ namespace SqlSugar
IStorageable<T> WhereColumns(Expression<Func<T, object>> columns);
IStorageable<T> SplitInsert(Func<StorageableInfo<T>, bool> conditions, string message=null);
IStorageable<T> SplitUpdate(Func<StorageableInfo<T>, bool> conditions, string message = null);
IStorageable<T> Saveable(string inserMessage = null, string updateMessage = null);
IStorageable<T> SplitError(Func<StorageableInfo<T>, bool> conditions, string message = null);
IStorageable<T> SplitIgnore(Func<StorageableInfo<T>, bool> conditions, string message = null);
IStorageable<T> SplitDelete(Func<StorageableInfo<T>, bool> conditions, string message = null);