mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 13:06:50 +08:00
Delete nav
This commit is contained in:
parent
adb786810f
commit
70b9e0561f
@ -10,8 +10,60 @@ namespace SqlSugar
|
||||
{
|
||||
private void DeleteManyToMany<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
|
||||
{
|
||||
var parentEntity = _ParentEntity;
|
||||
var parentList = _ParentList.Cast<T>().ToList();
|
||||
var parentPkColumn = parentEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true);
|
||||
var parentNavigateProperty = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == name);
|
||||
var thisEntity = this._Context.EntityMaintenance.GetEntityInfo<TChild>();
|
||||
var thisPkColumn = thisEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true);
|
||||
Check.Exception(thisPkColumn == null, $"{thisPkColumn.EntityName} need primary key", $"{thisPkColumn.EntityName}需要主键");
|
||||
Check.Exception(parentPkColumn == null, $"{parentPkColumn.EntityName} need primary key", $"{parentPkColumn.EntityName}需要主键");
|
||||
var mappingType = parentNavigateProperty.Navigat.MappingType;
|
||||
var mappingEntity = this._Context.EntityMaintenance.GetEntityInfo(mappingType);
|
||||
var mappingA = mappingEntity.Columns.FirstOrDefault(x => x.PropertyName == parentNavigateProperty.Navigat.MappingAId);
|
||||
var mappingB = mappingEntity.Columns.FirstOrDefault(x => x.PropertyName == parentNavigateProperty.Navigat.MappingBId);
|
||||
var mappingPk = mappingEntity.Columns
|
||||
.Where(it => it.PropertyName != mappingA.PropertyName)
|
||||
.Where(it => it.PropertyName != mappingB.PropertyName)
|
||||
.Where(it => it.IsPrimarykey && !it.IsIdentity && it.OracleSequenceName.IsNullOrEmpty()).FirstOrDefault();
|
||||
|
||||
if (IsDeleteA())
|
||||
{
|
||||
if (!_IsDeletedParant)
|
||||
SetContext(() => this._Context.Deleteable(parentList).ExecuteCommand());
|
||||
}
|
||||
|
||||
var aids = _ParentList.Select(it => parentPkColumn.PropertyInfo.GetValue(it)).ToList();
|
||||
var bids = _Context.Queryable<object>().AS(mappingEntity.DbTableName).In(mappingA.DbColumnName, aids)
|
||||
.Select(mappingB.DbColumnName).ToDataTable()
|
||||
.Rows.Cast<System.Data.DataRow>().Select(it => it[0]).ToList();
|
||||
|
||||
|
||||
var childList = this._Context.Queryable<TChild>().In(thisPkColumn.DbColumnName, bids).ToList();
|
||||
|
||||
|
||||
if (IsDeleteB())
|
||||
{
|
||||
SetContext(() => _Context.Deleteable(childList).ExecuteCommand());
|
||||
}
|
||||
|
||||
this._ParentList = childList.Cast<object>().ToList();
|
||||
this._ParentPkColumn = thisPkColumn;
|
||||
this._IsDeletedParant = true;
|
||||
|
||||
SetContext(() => _Context.Deleteable<object>().AS(mappingEntity.DbTableName).In(
|
||||
mappingA.DbColumnName, aids
|
||||
).ExecuteCommand());
|
||||
|
||||
}
|
||||
|
||||
private bool IsDeleteA()
|
||||
{
|
||||
return deleteNavOptions != null && deleteNavOptions.ManyToMayIsDeleteA;
|
||||
}
|
||||
private bool IsDeleteB()
|
||||
{
|
||||
return deleteNavOptions != null && deleteNavOptions.ManyToMayIsDeleteB;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ namespace SqlSugar
|
||||
{
|
||||
public partial class DeleteNavProvider<Root, T> where T : class, new() where Root : class, new()
|
||||
{
|
||||
internal DeleteNavOptions deleteNavOptions;
|
||||
public List<Root> _Roots { get; set; }
|
||||
public List<object> _ParentList { get; set; }
|
||||
public List<object> _RootList { get; set; }
|
||||
|
@ -31,6 +31,12 @@ namespace SqlSugar
|
||||
result.Context = this.Context;
|
||||
return result;
|
||||
}
|
||||
public DeleteNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, List<TChild>>> expression,DeleteNavOptions deleteNavOptions) where TChild : class, new()
|
||||
{
|
||||
var result= Include(expression);
|
||||
deleteNavProvider.deleteNavOptions = deleteNavOptions;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
public class DeleteNavTask<Root, T> where T : class, new() where Root : class, new()
|
||||
{
|
||||
@ -52,6 +58,18 @@ namespace SqlSugar
|
||||
result.Context = this.Context;
|
||||
return result;
|
||||
}
|
||||
public DeleteNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression,DeleteNavOptions deleteNavOptions) where TChild : class, new()
|
||||
{
|
||||
DeleteNavTask<Root, TChild> result = new DeleteNavTask<Root, TChild>();
|
||||
Func<DeleteNavProvider<Root, TChild>> func = () => {
|
||||
var dev = PreFunc();
|
||||
dev.deleteNavOptions = deleteNavOptions;
|
||||
return dev.ThenInclude(expression);
|
||||
};
|
||||
result.PreFunc = func;
|
||||
result.Context = this.Context;
|
||||
return result;
|
||||
}
|
||||
public DeleteNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, TChild>> expression) where TChild : class, new()
|
||||
{
|
||||
return AsNav().ThenInclude(expression);
|
||||
|
14
Src/Asp.Net/SqlSugar/Entities/DeleteNavOptions.cs
Normal file
14
Src/Asp.Net/SqlSugar/Entities/DeleteNavOptions.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class DeleteNavOptions
|
||||
{
|
||||
public bool ManyToMayIsDeleteA { get; set; }
|
||||
public bool ManyToMayIsDeleteB { get; set; }
|
||||
}
|
||||
}
|
@ -116,6 +116,7 @@
|
||||
<Compile Include="Abstract\SugarProvider\SqlSugarCoreProvider.cs" />
|
||||
<Compile Include="Abstract\UpdateProvider\SplitTableUpdateByObjectProvider.cs" />
|
||||
<Compile Include="Entities\DbFastestProperties.cs" />
|
||||
<Compile Include="Entities\DeleteNavOptions.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubWithNoLock.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubEnableTableFilter.cs" />
|
||||
<Compile Include="Json2Sql\Entities\JsonDeleteResult.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user