Update db.UpdateNav

This commit is contained in:
sunkaixuan 2022-07-17 00:11:17 +08:00
parent aa151d7804
commit 8a528196ff
5 changed files with 93 additions and 6 deletions

View File

@ -10,7 +10,6 @@ namespace SqlSugar
{
private void UpdateManyToMany<TChild>(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<TChild>)items);
InsertDatas(children, thisPkColumn);
if (this._Options != null && this._Options.ManyToManyIsUpdateB)
{
InsertDatas(children, thisPkColumn);
}
else
{
_ParentList = children.Cast<object>().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<object>().AS(mappingEntity.DbTableName).In(mappingA.DbColumnName, ids).ExecuteCommand();
this._Context.Insertable(mappgingTables).AS(mappingEntity.DbTableName).ExecuteCommand();
_ParentEntity = thisEntity;
}
private void SetMappingTableDefaultValue(EntityColumnInfo mappingPk, Dictionary<string, object> keyValuePairs)

View File

@ -17,7 +17,7 @@ namespace SqlSugar
public EntityColumnInfo _ParentPkColumn { get; set; }
public SqlSugarProvider _Context { get; set; }
public UpdateNavOptions _Options { get; set; }
public UpdateNavProvider<Root, Root> AsNav()
{
return new UpdateNavProvider<Root, Root>
@ -30,6 +30,28 @@ namespace SqlSugar
};
}
public UpdateNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
{
return _ThenInclude(expression);
}
public UpdateNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new()
{
return _ThenInclude(expression);
}
public UpdateNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression,UpdateNavOptions options) where TChild : class, new()
{
_Options= options;
return _ThenInclude(expression);
}
public UpdateNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression, UpdateNavOptions options) where TChild : class, new()
{
_Options = options;
return _ThenInclude(expression);
}
private UpdateNavProvider<Root, TChild> _ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
{
InitParentList();
var name = ExpressionTool.GetMemberName(expression);
@ -52,7 +74,8 @@ namespace SqlSugar
}
return GetResult<TChild>();
}
public UpdateNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new()
private UpdateNavProvider<Root, TChild> _ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new()
{
InitParentList();
var name = ExpressionTool.GetMemberName(expression);

View File

@ -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<object>().ToList();
_ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<Root>();
}

View File

@ -31,11 +31,33 @@ namespace SqlSugar
result.Context = this.Context;
return result;
}
public UpdateNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, TChild>> expression,UpdateNavOptions options) where TChild : class, new()
{
this.Context = UpdateNavProvider._Context;
UpdateNavTask<Root, TChild> result = new UpdateNavTask<Root, TChild>();
Func<UpdateNavProvider<Root, TChild>> func = () => UpdateNavProvider.ThenInclude(expression,options);
result.PreFunc = func;
result.Context = this.Context;
return result;
}
public UpdateNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, List<TChild>>> expression, UpdateNavOptions options) where TChild : class, new()
{
this.Context = UpdateNavProvider._Context;
UpdateNavTask<Root, TChild> result = new UpdateNavTask<Root, TChild>();
Func<UpdateNavProvider<Root, TChild>> func = () => UpdateNavProvider.ThenInclude(expression,options);
result.PreFunc = func;
result.Context = this.Context;
return result;
}
}
public class UpdateNavTask<Root, T> where T : class, new() where Root : class, new()
{
public SqlSugarProvider Context { get; set; }
public Func<UpdateNavProvider<Root, T>> PreFunc { get; set; }
#region +1
public UpdateNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
{
UpdateNavTask<Root, TChild> result = new UpdateNavTask<Root, TChild>();
@ -60,6 +82,38 @@ namespace SqlSugar
{
return AsNav().ThenInclude(expression);
}
#endregion
#region +2
public UpdateNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression, UpdateNavOptions options) where TChild : class, new()
{
UpdateNavTask<Root, TChild> result = new UpdateNavTask<Root, TChild>();
Func<UpdateNavProvider<Root, TChild>> func = () => PreFunc().ThenInclude(expression, options);
result.PreFunc = func;
result.Context = this.Context;
return result;
}
public UpdateNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression, UpdateNavOptions options) where TChild : class, new()
{
UpdateNavTask<Root, TChild> result = new UpdateNavTask<Root, TChild>();
Func<UpdateNavProvider<Root, TChild>> func = () => PreFunc().ThenInclude(expression, options);
result.PreFunc = func;
result.Context = this.Context;
return result;
}
public UpdateNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, TChild>> expression, UpdateNavOptions options) where TChild : class, new()
{
return AsNav().ThenInclude(expression, options);
}
public UpdateNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, List<TChild>>> expression, UpdateNavOptions options) where TChild : class, new()
{
return AsNav().ThenInclude(expression, options);
}
#endregion
public bool ExecuteCommand()
{
var hasTran = this.Context.Ado.Transaction != null;