Browse Source

Update db.UpdateNav

SqlSugar5
sunkaixuan 2 years ago
parent
commit
8a528196ff
  1. 11
      Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavManyToMany.cs
  2. 27
      Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavProvider.cs
  3. 5
      Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavProviderHelper.cs
  4. 54
      Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavTask.cs
  5. 2
      Src/Asp.Net/SqlSugar/Entities/DeleteNavOptions.cs

11
Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavManyToMany.cs

@ -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)

27
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<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);

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

54
Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavTask.cs

@ -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;

2
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; }

Loading…
Cancel
Save