Browse Source

Update db.UpdateableNav (OneToMany)

pull/23/head
sunkaixuan 2 years ago
parent
commit
a362cb53c7
  1. 53
      Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavOneToMany.cs

53
Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavOneToMany.cs

@ -68,22 +68,69 @@ namespace SqlSugar
var oneToManys = thisEntity.Columns.Where(it => it.Navigat != null && it.Navigat.NavigatType == NavigateType.OneToMany).ToList(); var oneToManys = thisEntity.Columns.Where(it => it.Navigat != null && it.Navigat.NavigatType == NavigateType.OneToMany).ToList();
foreach (var oneToMany in oneToManys) foreach (var oneToMany in oneToManys)
{ {
var fkFieldName = oneToMany.Navigat.Name2?? thisEntity.Columns.FirstOrDefault(it=>it.IsPrimarykey).PropertyName; var fkFieldName = oneToMany.Navigat.Name2 ?? thisEntity.Columns.FirstOrDefault(it => it.IsPrimarykey).PropertyName;
var fkDbColumnName = thisEntity.Columns.FirstOrDefault(it => it.PropertyName == fkFieldName).DbColumnName; var fkDbColumnName = thisEntity.Columns.FirstOrDefault(it => it.PropertyName == fkFieldName).DbColumnName;
var fks= this._Context.Queryable<object>() var fks = this._Context.Queryable<object>()
.AS(thisEntity.DbTableName) .AS(thisEntity.DbTableName)
.In(fkName, ids.Distinct().ToList()).Select(fkDbColumnName).ToDataTable().Rows.Cast<System.Data.DataRow>().Select(x=>x[0]).ToArray(); .In(fkName, ids.Distinct().ToList()).Select(fkDbColumnName).ToDataTable().Rows.Cast<System.Data.DataRow>().Select(x => x[0]).ToArray();
var type = oneToMany.PropertyInfo.PropertyType.GenericTypeArguments[0]; var type = oneToMany.PropertyInfo.PropertyType.GenericTypeArguments[0];
var entity = this._Context.EntityMaintenance.GetEntityInfo(type); var entity = this._Context.EntityMaintenance.GetEntityInfo(type);
var id = oneToMany.Navigat.Name; var id = oneToMany.Navigat.Name;
var column = entity.Columns.FirstOrDefault(it => it.PropertyName == id).DbColumnName; var column = entity.Columns.FirstOrDefault(it => it.PropertyName == id).DbColumnName;
DeleteChild(fks, entity, column);
this._Context.Deleteable<object>() this._Context.Deleteable<object>()
.AS(entity.DbTableName) .AS(entity.DbTableName)
.In(column, fks.Distinct().ToList()).ExecuteCommand(); .In(column, fks.Distinct().ToList()).ExecuteCommand();
} }
} }
private void DeleteChild(object[] fks, EntityInfo entity, string column)
{
var childs = entity.Columns.Where(it => it.Navigat != null && it.Navigat?.NavigatType == NavigateType.OneToMany).ToList();
if (childs.Any())
{
var pkColumn = entity.Columns.First(it => it.IsPrimarykey);
var pkIds = this._Context.Queryable<object>()
.AS(entity.DbTableName)
.In(column, fks.Distinct().ToList())
.Select(pkColumn.DbColumnName).ToDataTable().Rows
.Cast<System.Data.DataRow>().Select(it => it[0]).ToList();
DeleteChildChild(pkIds, childs);
}
}
int childIndex = 0;
private void DeleteChildChild(List<object> ids, List<EntityColumnInfo> childs)
{
childIndex++;
if (childIndex > 4)
{
Check.ExceptionEasy("Removing too many levels", "安全机制限制删除脏数据层级不能超过7层");
}
foreach (var columnInfo in childs)
{
var navigat = columnInfo.Navigat;
var type = columnInfo.PropertyInfo.PropertyType.GenericTypeArguments[0];
var thisEntity = this._Context.EntityMaintenance.GetEntityInfo(type);
var fkColumn = thisEntity.Columns.FirstOrDefault(it => navigat.Name.EqualCase(it.PropertyName));
var thisPkColumn = thisEntity.Columns.FirstOrDefault(it => it.IsPrimarykey);
var childs2 = thisEntity.Columns.Where(it => it.Navigat != null && it.Navigat?.NavigatType == NavigateType.OneToMany).ToList(); ;
if (childs2.Any())
{
var pkIds = _Context.Queryable<object>().AS(thisEntity.DbTableName)
.In(fkColumn.DbColumnName, ids)
.Select(thisPkColumn.DbColumnName).ToDataTable().Rows
.Cast<System.Data.DataRow>().Select(it => it[0]).ToList();
DeleteChildChild(pkIds, childs2);
}
_Context.Deleteable<object>().AS(thisEntity.DbTableName).In(fkColumn.DbColumnName, ids).ExecuteCommand();
}
}
private EntityColumnInfo GetParentPkColumn() private EntityColumnInfo GetParentPkColumn()
{ {
EntityColumnInfo parentPkColumn = _ParentPkColumn; EntityColumnInfo parentPkColumn = _ParentPkColumn;

Loading…
Cancel
Save