mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-05-18 05:59:34 +08:00
Update : db.UpdateNav
This commit is contained in:
parent
629a9c8166
commit
6f30c0232d
@ -11,9 +11,100 @@ namespace SqlSugar
|
|||||||
public NavigateType? _NavigateType { get; set; }
|
public NavigateType? _NavigateType { get; set; }
|
||||||
private void UpdateOneToMany<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
|
private void UpdateOneToMany<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
|
||||||
{
|
{
|
||||||
DeleteInsert<TChild>(name, nav);
|
if (_Options?.OneToManyInsertOrUpdate == true)
|
||||||
|
{
|
||||||
|
InsertOrUpdate<TChild>(name,nav);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DeleteInsert<TChild>(name, nav);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void InsertOrUpdate<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
|
||||||
|
{
|
||||||
|
List<TChild> children = new List<TChild>();
|
||||||
|
var parentEntity = _ParentEntity;
|
||||||
|
var parentList = _ParentList;
|
||||||
|
var parentNavigateProperty = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == name);
|
||||||
|
var thisEntity = this._Context.EntityMaintenance.GetEntityInfo<TChild>();
|
||||||
|
var thisPkColumn = GetPkColumnByNav2(thisEntity, nav);
|
||||||
|
var thisFkColumn = GetFKColumnByNav(thisEntity, nav);
|
||||||
|
EntityColumnInfo parentPkColumn = GetParentPkColumn();
|
||||||
|
EntityColumnInfo parentNavColumn = GetParentPkNavColumn(nav);
|
||||||
|
if (parentNavColumn != null)
|
||||||
|
{
|
||||||
|
parentPkColumn = parentNavColumn;
|
||||||
|
}
|
||||||
|
if (ParentIsPk(parentNavigateProperty))
|
||||||
|
{
|
||||||
|
parentPkColumn = this._ParentEntity.Columns.FirstOrDefault(it => it.IsPrimarykey);
|
||||||
|
}
|
||||||
|
var ids = new List<object>();
|
||||||
|
foreach (var item in parentList)
|
||||||
|
{
|
||||||
|
var parentValue = parentPkColumn.PropertyInfo.GetValue(item);
|
||||||
|
var childs = parentNavigateProperty.PropertyInfo.GetValue(item) as List<TChild>;
|
||||||
|
if (childs != null)
|
||||||
|
{
|
||||||
|
foreach (var child in childs)
|
||||||
|
{
|
||||||
|
thisFkColumn.PropertyInfo.SetValue(child, parentValue, null);
|
||||||
|
}
|
||||||
|
children.AddRange(childs);
|
||||||
|
}
|
||||||
|
ids.Add(parentValue);
|
||||||
|
if (_Options?.OneToManyNoDeleteNull == true && childs == null)
|
||||||
|
{
|
||||||
|
ids.Remove(parentValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (NotAny(name))
|
||||||
|
{
|
||||||
|
DeleteMany(thisEntity, ids, thisFkColumn.DbColumnName);
|
||||||
|
if (this._Options?.OneToManyEnableLogicDelete == true)
|
||||||
|
{
|
||||||
|
var locgicColumn = thisEntity.Columns.FirstOrDefault(it => it.PropertyName.EqualCase("IsDeleted") || it.PropertyName.EqualCase("IsDelete"));
|
||||||
|
Check.ExceptionEasy(
|
||||||
|
locgicColumn == null,
|
||||||
|
thisEntity.EntityName + "Logical deletion requires the entity to have the IsDeleted property",
|
||||||
|
thisEntity.EntityName + "假删除需要实体有IsDeleted属性");
|
||||||
|
List<IConditionalModel> conditionalModels = new List<IConditionalModel>();
|
||||||
|
conditionalModels.Add(new ConditionalModel()
|
||||||
|
{
|
||||||
|
FieldName = thisFkColumn.DbColumnName,
|
||||||
|
FieldValue = string.Join(",", ids.Distinct()),
|
||||||
|
ConditionalType = ConditionalType.In,
|
||||||
|
CSharpTypeName = thisFkColumn?.PropertyInfo?.PropertyType?.Name
|
||||||
|
});
|
||||||
|
var sqlObj = _Context.Queryable<object>().SqlBuilder.ConditionalModelToSql(conditionalModels);
|
||||||
|
this._Context.Updateable<object>()
|
||||||
|
.AS(thisEntity.DbTableName)
|
||||||
|
.Where(sqlObj.Key, sqlObj.Value)
|
||||||
|
.SetColumns(locgicColumn.DbColumnName, true)
|
||||||
|
.ExecuteCommand();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var list=this._Context.Queryable<TChild>()
|
||||||
|
.AS(thisEntity.DbTableName)
|
||||||
|
.In(thisFkColumn.DbColumnName, ids.Distinct().ToList())
|
||||||
|
.ToList();
|
||||||
|
List<TChild> result = GetNoExistsId(list, children, thisPkColumn.PropertyName);
|
||||||
|
if (result.Any())
|
||||||
|
{
|
||||||
|
this._Context.Deleteable(result).ExecuteCommand();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_NavigateType = NavigateType.OneToMany;
|
||||||
|
InsertDatas(children, thisPkColumn);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this._ParentList = children.Cast<object>().ToList();
|
||||||
|
}
|
||||||
|
_NavigateType = null;
|
||||||
|
SetNewParent<TChild>(thisEntity, thisPkColumn);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DeleteInsert<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
|
private void DeleteInsert<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
|
||||||
{
|
{
|
||||||
List<TChild> children = new List<TChild>();
|
List<TChild> children = new List<TChild>();
|
||||||
@ -197,5 +288,33 @@ namespace SqlSugar
|
|||||||
this._ParentEntity = entityInfo;
|
this._ParentEntity = entityInfo;
|
||||||
this._ParentPkColumn = entityColumnInfo;
|
this._ParentPkColumn = entityColumnInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<TChild> GetNoExistsId<TChild>(List<TChild> old, List<TChild> newList, string pkName)
|
||||||
|
{
|
||||||
|
List<TChild> result = new List<TChild>();
|
||||||
|
|
||||||
|
// 将newList中的主键属性转换为字符串集合
|
||||||
|
var newIds = newList.Select(item => GetPropertyValueAsString(item, pkName)).ToList();
|
||||||
|
|
||||||
|
// 获取在old中但不在newList中的主键属性值
|
||||||
|
result = old.Where(item => !newIds.Contains(GetPropertyValueAsString(item, pkName)))
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取对象的属性值
|
||||||
|
private string GetPropertyValueAsString<TChild>(TChild item, string propertyName)
|
||||||
|
{
|
||||||
|
var property = item.GetType().GetProperty(propertyName);
|
||||||
|
if (property != null)
|
||||||
|
{
|
||||||
|
return property.GetValue(item, null)+"";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Property '{propertyName}' not found on type {item.GetType().Name}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user