mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-18 17:48:11 +08:00
Update db.UpdateNav
This commit is contained in:
@@ -16,7 +16,8 @@ namespace SqlSugar
|
||||
public EntityInfo _ParentEntity { get; set; }
|
||||
public EntityColumnInfo _ParentPkColumn { get; set; }
|
||||
public SqlSugarProvider _Context { get; set; }
|
||||
|
||||
public NavigateType? _NavigateType { get; set; }
|
||||
public bool IsFirst { get; set; }
|
||||
|
||||
public InsertNavProvider<Root, Root> AsNav()
|
||||
{
|
||||
@@ -30,8 +31,13 @@ namespace SqlSugar
|
||||
}
|
||||
public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
|
||||
{
|
||||
InitParentList();
|
||||
|
||||
var name = ExpressionTool.GetMemberName(expression);
|
||||
if (this._ParentEntity == null)
|
||||
{
|
||||
this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<Root>();
|
||||
this.IsFirst = true;
|
||||
}
|
||||
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
|
||||
if (nav.Navigat == null)
|
||||
{
|
||||
@@ -39,37 +45,51 @@ namespace SqlSugar
|
||||
}
|
||||
if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
|
||||
{
|
||||
InitParentList();
|
||||
InsertOneToOne<TChild>(name, nav);
|
||||
}
|
||||
else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
|
||||
{
|
||||
_NavigateType = NavigateType.OneToMany;
|
||||
InitParentList();
|
||||
InsertOneToMany<TChild>(name, nav);
|
||||
}
|
||||
else
|
||||
{
|
||||
InitParentList();
|
||||
InsertManyToMany<TChild>(name, nav);
|
||||
}
|
||||
return GetResult<TChild>();
|
||||
}
|
||||
public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T,List<TChild>>> expression) where TChild : class, new()
|
||||
{
|
||||
InitParentList();
|
||||
var name = ExpressionTool.GetMemberName(expression);
|
||||
if (this._ParentEntity == null)
|
||||
{
|
||||
this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<Root>();
|
||||
IsFirst = true;
|
||||
}
|
||||
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
|
||||
;
|
||||
|
||||
if (nav.Navigat == null)
|
||||
{
|
||||
Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性");
|
||||
}
|
||||
if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
|
||||
{
|
||||
InitParentList();
|
||||
InsertOneToOne<TChild>(name, nav);
|
||||
}
|
||||
else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
|
||||
{
|
||||
_NavigateType = NavigateType.OneToMany;
|
||||
InitParentList();
|
||||
InsertOneToMany<TChild>(name, nav);
|
||||
}
|
||||
else
|
||||
{
|
||||
InitParentList();
|
||||
InsertManyToMany<TChild>(name, nav);
|
||||
}
|
||||
return GetResult<TChild>();
|
||||
|
@@ -26,7 +26,7 @@ namespace SqlSugar
|
||||
var pkColumn = this._Context.EntityMaintenance.GetEntityInfo<T>().Columns.FirstOrDefault(it => it.IsPrimarykey);
|
||||
this._ParentPkColumn = pkColumn;
|
||||
}
|
||||
|
||||
IsFirst = false;
|
||||
}
|
||||
|
||||
private InsertNavProvider<Root, TChild> GetResult<TChild>() where TChild : class, new()
|
||||
@@ -81,11 +81,26 @@ namespace SqlSugar
|
||||
children = children.Distinct().ToList();
|
||||
var x = this._Context.Storageable(children).WhereColumns(new string[] { pkColumn.PropertyName }).ToStorage();
|
||||
var insertData = children = x.InsertList.Select(it => it.Item).ToList();
|
||||
if (_NavigateType == NavigateType.OneToMany&&IsFirst==false)
|
||||
{
|
||||
var updateData = x.UpdateList.Select(it => it.Item).ToList();
|
||||
ClearPk(updateData, pkColumn);
|
||||
insertData.AddRange(updateData);
|
||||
}
|
||||
Check.ExceptionEasy(pkColumn==null&&NavColumn==null,$"The entity is invalid",$"实体错误无法使用导航");
|
||||
InitData(pkColumn, insertData);
|
||||
this._ParentList = children.Cast<object>().ToList();
|
||||
}
|
||||
|
||||
private void ClearPk<TChild>(List<TChild> updateData, EntityColumnInfo pkColumn) where TChild : class, new()
|
||||
{
|
||||
foreach (var child in updateData)
|
||||
{
|
||||
var defaultValue =UtilMethods.DefaultForType(pkColumn.PropertyInfo.PropertyType);
|
||||
pkColumn.PropertyInfo.SetValue(child, defaultValue);
|
||||
}
|
||||
}
|
||||
|
||||
private void InitData<TChild>(EntityColumnInfo pkColumn, List<TChild> insertData) where TChild : class, new()
|
||||
{
|
||||
if (pkColumn.IsIdentity || pkColumn.OracleSequenceName.HasValue())
|
||||
|
Reference in New Issue
Block a user