mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-12-26 14:15:50 +08:00
Update db.Updateable(+2)
This commit is contained in:
@@ -18,6 +18,7 @@ namespace SqlSugar
|
||||
public SqlSugarProvider _Context { get; set; }
|
||||
|
||||
public UpdateNavOptions _Options { get; set; }
|
||||
public bool IsFirst { get; set; }
|
||||
public UpdateNavProvider<Root, Root> AsNav()
|
||||
{
|
||||
return new UpdateNavProvider<Root, Root>
|
||||
@@ -54,6 +55,7 @@ namespace SqlSugar
|
||||
private UpdateNavProvider<Root, TChild> _ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
|
||||
{
|
||||
var isRoot = _RootList == null;
|
||||
IsFirst = isRoot && this._ParentList == null;
|
||||
InitParentList();
|
||||
var name = ExpressionTool.GetMemberName(expression);
|
||||
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
|
||||
@@ -62,6 +64,7 @@ namespace SqlSugar
|
||||
Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性");
|
||||
}
|
||||
UpdateRoot(isRoot, nav);
|
||||
IsFirst = false;
|
||||
if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
|
||||
{
|
||||
UpdateOneToOne<TChild>(name, nav);
|
||||
@@ -79,6 +82,7 @@ namespace SqlSugar
|
||||
private UpdateNavProvider<Root, TChild> _ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new()
|
||||
{
|
||||
var isRoot = _RootList == null;
|
||||
IsFirst = isRoot && this._ParentList == null;
|
||||
InitParentList();
|
||||
var name = ExpressionTool.GetMemberName(expression);
|
||||
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
|
||||
@@ -87,6 +91,7 @@ namespace SqlSugar
|
||||
Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性");
|
||||
}
|
||||
UpdateRoot(isRoot, nav);
|
||||
IsFirst = false;
|
||||
if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
|
||||
{
|
||||
UpdateOneToOne<TChild>(name, nav);
|
||||
@@ -121,12 +126,41 @@ namespace SqlSugar
|
||||
if (_Options != null && _Options.RootFunc != null)
|
||||
{
|
||||
var updateable = this._Context.Updateable(_Roots);
|
||||
var exp= _Options.RootFunc as Expression<Action<IUpdateable<Root>>>;
|
||||
var exp = _Options.RootFunc as Expression<Action<IUpdateable<Root>>>;
|
||||
Check.ExceptionEasy(exp == null, "UpdateOptions.RootFunc is error", "UpdateOptions.RootFunc");
|
||||
var com= exp.Compile();
|
||||
var com = exp.Compile();
|
||||
com(updateable);
|
||||
updateable.ExecuteCommand();
|
||||
}
|
||||
else if (IsFirst && _RootOptions != null)
|
||||
{
|
||||
var isInsert = _RootOptions.IsInsertRoot;
|
||||
if (isInsert)
|
||||
{
|
||||
var newRoots=new List<Root>();
|
||||
foreach (var item in _Roots)
|
||||
{
|
||||
var x = this._Context.Storageable(item).ToStorage();
|
||||
if (x.InsertList.HasValue())
|
||||
{
|
||||
newRoots.Add( x.AsInsertable.ExecuteReturnEntity());
|
||||
}
|
||||
else
|
||||
{
|
||||
x.AsUpdateable.ExecuteCommand();
|
||||
newRoots.Add(item);
|
||||
}
|
||||
}
|
||||
_ParentList=_RootList=newRoots.Cast<object>().ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
this._Context.Updateable(_Roots)
|
||||
.UpdateColumns(_RootOptions.UpdateColumns)
|
||||
.IgnoreColumns(_RootOptions.IgnoreColumns)
|
||||
.ExecuteCommand();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this._Context.Updateable(_Roots).ExecuteCommand();
|
||||
|
||||
@@ -343,8 +343,11 @@ namespace SqlSugar
|
||||
}
|
||||
public IUpdateable<T> UpdateColumns(string[] columns)
|
||||
{
|
||||
ThrowUpdateByExpression();
|
||||
this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it => GetPrimaryKeys().Select(iit => iit.ToLower()).Contains(it.DbColumnName.ToLower()) || columns.Contains(it.PropertyName, StringComparer.OrdinalIgnoreCase)).ToList();
|
||||
if (columns.HasValue())
|
||||
{
|
||||
ThrowUpdateByExpression();
|
||||
this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it => GetPrimaryKeys().Select(iit => iit.ToLower()).Contains(it.DbColumnName.ToLower()) || columns.Contains(it.PropertyName, StringComparer.OrdinalIgnoreCase)).ToList();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public IUpdateable<T> UpdateColumnsIF(bool isUpdateColumns, Expression<Func<T, object>> columns)
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace SqlSugar
|
||||
{
|
||||
public string IgnoreColumns { get; set; }
|
||||
public string[] UpdateColumns { get; set; }
|
||||
public bool IsInsert { get; set; }
|
||||
public bool IsInsertRoot { get; set; }
|
||||
}
|
||||
public class UpdateNavOptions
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user