From 8a528196ff23b136804c28b93a20e2dbd4da332f Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 17 Jul 2022 00:11:17 +0800 Subject: [PATCH] Update db.UpdateNav --- .../ExecuteNavProvider/UpdateNavManyToMany.cs | 11 +++- .../ExecuteNavProvider/UpdateNavProvider.cs | 27 +++++++++- .../UpdateNavProviderHelper.cs | 5 +- .../ExecuteNavProvider/UpdateNavTask.cs | 54 +++++++++++++++++++ .../SqlSugar/Entities/DeleteNavOptions.cs | 2 +- 5 files changed, 93 insertions(+), 6 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavManyToMany.cs b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavManyToMany.cs index 9b7534cef..01b622a5d 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavManyToMany.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavManyToMany.cs @@ -10,7 +10,6 @@ namespace SqlSugar { private void UpdateManyToMany(string name, EntityColumnInfo nav) where TChild : class, new() { - ; var parentEntity = _ParentEntity; var parentList = _ParentList; var parentPkColumn = parentEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true); @@ -33,7 +32,14 @@ namespace SqlSugar { var items = parentNavigateProperty.PropertyInfo.GetValue(item); var children = ((List)items); - InsertDatas(children, thisPkColumn); + if (this._Options != null && this._Options.ManyToManyIsUpdateB) + { + InsertDatas(children, thisPkColumn); + } + else + { + _ParentList = children.Cast().ToList(); + } var parentId = parentPkColumn.PropertyInfo.GetValue(item); foreach (var child in children) { @@ -51,6 +57,7 @@ namespace SqlSugar var ids = mappgingTables.Select(x => x[mappingA.DbColumnName]).ToList(); this._Context.Deleteable().AS(mappingEntity.DbTableName).In(mappingA.DbColumnName, ids).ExecuteCommand(); this._Context.Insertable(mappgingTables).AS(mappingEntity.DbTableName).ExecuteCommand(); + _ParentEntity = thisEntity; } private void SetMappingTableDefaultValue(EntityColumnInfo mappingPk, Dictionary keyValuePairs) diff --git a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavProvider.cs index 9bb718a5e..27e46656f 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavProvider.cs @@ -17,7 +17,7 @@ namespace SqlSugar public EntityColumnInfo _ParentPkColumn { get; set; } public SqlSugarProvider _Context { get; set; } - + public UpdateNavOptions _Options { get; set; } public UpdateNavProvider AsNav() { return new UpdateNavProvider @@ -30,6 +30,28 @@ namespace SqlSugar }; } public UpdateNavProvider ThenInclude(Expression> expression) where TChild : class, new() + { + return _ThenInclude(expression); + } + + public UpdateNavProvider ThenInclude(Expression>> expression) where TChild : class, new() + { + return _ThenInclude(expression); + } + + public UpdateNavProvider ThenInclude(Expression> expression,UpdateNavOptions options) where TChild : class, new() + { + _Options= options; + return _ThenInclude(expression); + } + + public UpdateNavProvider ThenInclude(Expression>> expression, UpdateNavOptions options) where TChild : class, new() + { + _Options = options; + return _ThenInclude(expression); + } + + private UpdateNavProvider _ThenInclude(Expression> expression) where TChild : class, new() { InitParentList(); var name = ExpressionTool.GetMemberName(expression); @@ -52,7 +74,8 @@ namespace SqlSugar } return GetResult(); } - public UpdateNavProvider ThenInclude(Expression>> expression) where TChild : class, new() + + private UpdateNavProvider _ThenInclude(Expression>> expression) where TChild : class, new() { InitParentList(); var name = ExpressionTool.GetMemberName(expression); diff --git a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavProviderHelper.cs b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavProviderHelper.cs index b77a57636..7a1c9537b 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavProviderHelper.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavProviderHelper.cs @@ -18,7 +18,10 @@ namespace SqlSugar { if (_RootList == null) { - this._Context.Updateable(_Roots).ExecuteCommand(); + if (_Options != null && _Options.ManyToManyIsUpdateA) + { + this._Context.Updateable(_Roots).ExecuteCommand(); + } _RootList = _ParentList = _Roots.Cast().ToList(); _ParentEntity = this._Context.EntityMaintenance.GetEntityInfo(); } diff --git a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavTask.cs b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavTask.cs index 1a6a5c9b5..26c2a63d3 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavTask.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavTask.cs @@ -31,11 +31,33 @@ namespace SqlSugar result.Context = this.Context; return result; } + + public UpdateNavTask Include(Expression> expression,UpdateNavOptions options) where TChild : class, new() + { + this.Context = UpdateNavProvider._Context; + UpdateNavTask result = new UpdateNavTask(); + Func> func = () => UpdateNavProvider.ThenInclude(expression,options); + result.PreFunc = func; + result.Context = this.Context; + return result; + } + public UpdateNavTask Include(Expression>> expression, UpdateNavOptions options) where TChild : class, new() + { + this.Context = UpdateNavProvider._Context; + UpdateNavTask result = new UpdateNavTask(); + Func> func = () => UpdateNavProvider.ThenInclude(expression,options); + result.PreFunc = func; + result.Context = this.Context; + return result; + } } public class UpdateNavTask where T : class, new() where Root : class, new() { public SqlSugarProvider Context { get; set; } public Func> PreFunc { get; set; } + + + #region +1 public UpdateNavTask ThenInclude(Expression> expression) where TChild : class, new() { UpdateNavTask result = new UpdateNavTask(); @@ -60,6 +82,38 @@ namespace SqlSugar { return AsNav().ThenInclude(expression); } + + #endregion + + + #region +2 + public UpdateNavTask ThenInclude(Expression> expression, UpdateNavOptions options) where TChild : class, new() + { + UpdateNavTask result = new UpdateNavTask(); + Func> func = () => PreFunc().ThenInclude(expression, options); + result.PreFunc = func; + result.Context = this.Context; + return result; + } + public UpdateNavTask ThenInclude(Expression>> expression, UpdateNavOptions options) where TChild : class, new() + { + UpdateNavTask result = new UpdateNavTask(); + Func> func = () => PreFunc().ThenInclude(expression, options); + result.PreFunc = func; + result.Context = this.Context; + return result; + } + public UpdateNavTask Include(Expression> expression, UpdateNavOptions options) where TChild : class, new() + { + return AsNav().ThenInclude(expression, options); + } + public UpdateNavTask Include(Expression>> expression, UpdateNavOptions options) where TChild : class, new() + { + return AsNav().ThenInclude(expression, options); + } + #endregion + + public bool ExecuteCommand() { var hasTran = this.Context.Ado.Transaction != null; diff --git a/Src/Asp.Net/SqlSugar/Entities/DeleteNavOptions.cs b/Src/Asp.Net/SqlSugar/Entities/DeleteNavOptions.cs index 53cd2818a..c4aadec75 100644 --- a/Src/Asp.Net/SqlSugar/Entities/DeleteNavOptions.cs +++ b/Src/Asp.Net/SqlSugar/Entities/DeleteNavOptions.cs @@ -10,7 +10,7 @@ namespace SqlSugar { public bool ManyToManyIsDeleteA { get; set; } public bool ManyToManyIsDeleteB { get; set; } - } + } public class UpdateNavOptions { public bool ManyToManyIsUpdateA { get; set; }