Update db.UpdateNav(list)

This commit is contained in:
sunkaixuan 2022-07-16 21:21:48 +08:00
parent 69080be4a6
commit f5e42c5fde
2 changed files with 14 additions and 17 deletions

View File

@ -24,6 +24,7 @@ namespace SqlSugar
{ {
parentPkColumn = parentNavColumn; parentPkColumn = parentNavColumn;
} }
var ids = new List<object>();
foreach (var item in parentList) foreach (var item in parentList)
{ {
var parentValue = parentPkColumn.PropertyInfo.GetValue(item); var parentValue = parentPkColumn.PropertyInfo.GetValue(item);
@ -33,10 +34,14 @@ namespace SqlSugar
foreach (var child in childs) foreach (var child in childs)
{ {
thisFkColumn.PropertyInfo.SetValue(child, parentValue, null); thisFkColumn.PropertyInfo.SetValue(child, parentValue, null);
ids.Add(parentValue);
} }
children.AddRange(childs); children.AddRange(childs);
} }
} }
this._Context.Deleteable<object>()
.AS(thisEntity.DbTableName)
.In(thisFkColumn.DbColumnName, ids.Distinct().ToList()).ExecuteCommand();
InsertDatas(children, thisPkColumn); InsertDatas(children, thisPkColumn);
SetNewParent<TChild>(thisEntity, thisPkColumn); SetNewParent<TChild>(thisEntity, thisPkColumn);
} }

View File

@ -18,7 +18,9 @@ namespace SqlSugar
{ {
if (_RootList == null) if (_RootList == null)
{ {
_RootList = _ParentList = GetRootList(_Roots).Cast<object>().ToList(); this._Context.Updateable(_Roots).ExecuteCommand();
_RootList = _ParentList = _Roots.Cast<object>().ToList();
_ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<Root>();
} }
else if (_ParentList == null) else if (_ParentList == null)
{ {
@ -26,7 +28,6 @@ namespace SqlSugar
var pkColumn = this._Context.EntityMaintenance.GetEntityInfo<T>().Columns.FirstOrDefault(it => it.IsPrimarykey); var pkColumn = this._Context.EntityMaintenance.GetEntityInfo<T>().Columns.FirstOrDefault(it => it.IsPrimarykey);
this._ParentPkColumn = pkColumn; this._ParentPkColumn = pkColumn;
} }
} }
private UpdateNavProvider<Root, TChild> GetResult<TChild>() where TChild : class, new() private UpdateNavProvider<Root, TChild> GetResult<TChild>() where TChild : class, new()
@ -42,17 +43,6 @@ namespace SqlSugar
}; };
} }
private List<Type> GetRootList<Type>(List<Type> datas) where Type : class, new()
{
List<Type> result = new List<Type>();
this._Context.InitMappingInfo<Type>();
var entity = this._Context.EntityMaintenance.GetEntityInfo<Type>();
var pkColumn = entity.Columns.FirstOrDefault(it => it.IsPrimarykey);
InsertDatas(datas, pkColumn);
this._ParentEntity = entity;
result = datas;
return result;
}
private void InsertIdentity<Type>(List<Type> datas) where Type : class, new() private void InsertIdentity<Type>(List<Type> datas) where Type : class, new()
{ {
@ -80,10 +70,12 @@ namespace SqlSugar
{ {
children = children.Distinct().ToList(); children = children.Distinct().ToList();
var x = this._Context.Storageable(children).WhereColumns(new string[] { pkColumn.PropertyName }).ToStorage(); var x = this._Context.Storageable(children).WhereColumns(new string[] { pkColumn.PropertyName }).ToStorage();
var UpdateData = children = x.UpdateList.Select(it => it.Item).ToList(); var insertData = x.InsertList.Select(it => it.Item).ToList();
var updateData = x.UpdateList.Select(it => it.Item).ToList();
Check.ExceptionEasy(pkColumn == null && NavColumn == null, $"The entity is invalid", $"实体错误无法使用导航"); Check.ExceptionEasy(pkColumn == null && NavColumn == null, $"The entity is invalid", $"实体错误无法使用导航");
InitData(pkColumn, UpdateData); x.AsUpdateable.ExecuteCommand();
this._ParentList = children.Cast<object>().ToList(); InitData(pkColumn, insertData);
this._ParentList = insertData.Union(updateData).Cast<object>().ToList();
} }
private void InitData<TChild>(EntityColumnInfo pkColumn, List<TChild> UpdateData) where TChild : class, new() private void InitData<TChild>(EntityColumnInfo pkColumn, List<TChild> UpdateData) where TChild : class, new()