Update Nav Insert

This commit is contained in:
sunkaixuan 2023-03-03 14:12:16 +08:00
parent 578bdd070b
commit 85733a79a4
6 changed files with 88 additions and 6 deletions

View File

@ -19,6 +19,8 @@ namespace SqlSugar
public NavigateType? _NavigateType { get; set; } public NavigateType? _NavigateType { get; set; }
public bool IsFirst { get; set; } public bool IsFirst { get; set; }
public InsertNavOptions _navOptions { get; set; } public InsertNavOptions _navOptions { get; set; }
public bool IsNav { get; internal set; }
internal NavContext NavContext { get; set; }
public InsertNavProvider<Root, Root> AsNav() public InsertNavProvider<Root, Root> AsNav()
{ {
@ -56,10 +58,12 @@ namespace SqlSugar
private InsertNavProvider<Root, TChild> _ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new() private InsertNavProvider<Root, TChild> _ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
{ {
var name = ExpressionTool.GetMemberName(expression); var name = ExpressionTool.GetMemberName(expression);
var isRoot = false;
if (this._ParentEntity == null) if (this._ParentEntity == null)
{ {
this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<Root>(); this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<Root>();
this.IsFirst = true; this.IsFirst = true;
isRoot = true;
} }
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name); var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
if (nav.Navigat == null) if (nav.Navigat == null)
@ -82,20 +86,21 @@ namespace SqlSugar
InitParentList(); InitParentList();
InsertManyToMany<TChild>(name, nav); InsertManyToMany<TChild>(name, nav);
} }
AddContextInfo(name,isRoot);
return GetResult<TChild>(); return GetResult<TChild>();
} }
private InsertNavProvider<Root, TChild> _ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new() private InsertNavProvider<Root, TChild> _ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new()
{ {
var name = ExpressionTool.GetMemberName(expression); var name = ExpressionTool.GetMemberName(expression);
var isRoot = false;
if (this._ParentEntity == null) if (this._ParentEntity == null)
{ {
this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<Root>(); this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<Root>();
IsFirst = true; IsFirst = true;
isRoot = true;
} }
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name); var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
;
if (nav.Navigat == null) if (nav.Navigat == null)
{ {
Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性"); Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性");
@ -116,7 +121,25 @@ namespace SqlSugar
InitParentList(); InitParentList();
InsertManyToMany<TChild>(name, nav); InsertManyToMany<TChild>(name, nav);
} }
AddContextInfo(name,isRoot);
return GetResult<TChild>(); return GetResult<TChild>();
} }
private void AddContextInfo(string name,bool isRoot)
{
if (IsNav || isRoot)
{
if (this.NavContext != null && this.NavContext.Items != null)
{
this.NavContext.Items.Add(new NavContextItem() { Level = 0, RootName = name });
}
}
}
private bool NotAny(string name)
{
if (IsFirst) return true;
if (this.NavContext == null) return true;
return this.NavContext?.Items?.Any(it => it.RootName == name) == false;
}
} }
} }

View File

@ -42,7 +42,14 @@ namespace SqlSugar
} }
} }
Check.ExceptionEasy(thisPkColumn == null, $"{thisEntity.EntityName}need primary key", $"实体{thisEntity.EntityName}需要主键"); Check.ExceptionEasy(thisPkColumn == null, $"{thisEntity.EntityName}need primary key", $"实体{thisEntity.EntityName}需要主键");
InsertDatas(children, thisPkColumn); if (NotAny(name))
{
InsertDatas(children, thisPkColumn);
}
else
{
this._ParentList = children.Cast<object>().ToList();
}
SetNewParent<TChild>(thisEntity, thisPkColumn); SetNewParent<TChild>(thisEntity, thisPkColumn);
} }

View File

@ -12,42 +12,51 @@ namespace SqlSugar
internal SqlSugarProvider Context { get; set; } internal SqlSugarProvider Context { get; set; }
internal InsertNavProvider<Root, Root> insertNavProvider { get; set; } internal InsertNavProvider<Root, Root> insertNavProvider { get; set; }
internal NavContext NavContext { get; set; }
public InsertNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, TChild>> expression) where TChild : class, new() public InsertNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, TChild>> expression) where TChild : class, new()
{ {
this.Context = insertNavProvider._Context; this.Context = insertNavProvider._Context;
insertNavProvider.NavContext = this.NavContext;
InsertNavTask<Root, TChild> result = new InsertNavTask<Root, TChild>(); InsertNavTask<Root, TChild> result = new InsertNavTask<Root, TChild>();
Func<InsertNavProvider<Root, TChild>> func = () => insertNavProvider.ThenInclude(expression); Func<InsertNavProvider<Root, TChild>> func = () => insertNavProvider.ThenInclude(expression);
result.PreFunc = func; result.PreFunc = func;
result.Context = this.Context; result.Context = this.Context;
result.NavContext = this.NavContext;
return result; return result;
} }
public InsertNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, List<TChild>>> expression) where TChild : class, new() public InsertNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, List<TChild>>> expression) where TChild : class, new()
{ {
this.Context = insertNavProvider._Context; this.Context = insertNavProvider._Context;
insertNavProvider.NavContext = this.NavContext;
InsertNavTask<Root, TChild> result = new InsertNavTask<Root, TChild>(); InsertNavTask<Root, TChild> result = new InsertNavTask<Root, TChild>();
Func<InsertNavProvider<Root, TChild>> func = () => insertNavProvider.ThenInclude(expression); Func<InsertNavProvider<Root, TChild>> func = () => insertNavProvider.ThenInclude(expression);
result.PreFunc = func; result.PreFunc = func;
result.Context = this.Context; result.Context = this.Context;
result.NavContext = this.NavContext;
return result; return result;
} }
public InsertNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, TChild>> expression,InsertNavOptions options) where TChild : class, new() public InsertNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, TChild>> expression,InsertNavOptions options) where TChild : class, new()
{ {
this.Context = insertNavProvider._Context; this.Context = insertNavProvider._Context;
insertNavProvider.NavContext = this.NavContext;
InsertNavTask<Root, TChild> result = new InsertNavTask<Root, TChild>(); InsertNavTask<Root, TChild> result = new InsertNavTask<Root, TChild>();
Func<InsertNavProvider<Root, TChild>> func = () => insertNavProvider.ThenInclude(expression, options); Func<InsertNavProvider<Root, TChild>> func = () => insertNavProvider.ThenInclude(expression, options);
result.PreFunc = func; result.PreFunc = func;
result.Context = this.Context; result.Context = this.Context;
result.NavContext = this.NavContext;
return result; return result;
} }
public InsertNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, List<TChild>>> expression, InsertNavOptions options) where TChild : class, new() public InsertNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, List<TChild>>> expression, InsertNavOptions options) where TChild : class, new()
{ {
this.Context = insertNavProvider._Context; this.Context = insertNavProvider._Context;
insertNavProvider.NavContext =this.NavContext;
InsertNavTask<Root, TChild> result = new InsertNavTask<Root, TChild>(); InsertNavTask<Root, TChild> result = new InsertNavTask<Root, TChild>();
Func<InsertNavProvider<Root, TChild>> func = () => insertNavProvider.ThenInclude(expression, options); Func<InsertNavProvider<Root, TChild>> func = () => insertNavProvider.ThenInclude(expression, options);
result.PreFunc = func; result.PreFunc = func;
result.Context = this.Context; result.Context = this.Context;
result.NavContext = this.NavContext;
return result; return result;
} }
} }
@ -55,20 +64,33 @@ namespace SqlSugar
{ {
public SqlSugarProvider Context { get; set; } public SqlSugarProvider Context { get; set; }
public Func<InsertNavProvider<Root, T>> PreFunc { get; set; } public Func<InsertNavProvider<Root, T>> PreFunc { get; set; }
internal NavContext NavContext { get; set; }
public InsertNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new() public InsertNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
{ {
InsertNavTask<Root, TChild> result = new InsertNavTask<Root, TChild>(); InsertNavTask<Root, TChild> result = new InsertNavTask<Root, TChild>();
Func<InsertNavProvider<Root, TChild>> func = () => PreFunc().ThenInclude(expression); Func<InsertNavProvider<Root, TChild>> func = () => {
var nav = PreFunc().ThenInclude(expression);
nav.NavContext = this.NavContext;
return nav;
};
result.PreFunc = func; result.PreFunc = func;
result.Context = this.Context; result.Context = this.Context;
result.NavContext = this.NavContext;
return result; return result;
} }
public InsertNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new() public InsertNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new()
{ {
InsertNavTask<Root, TChild> result = new InsertNavTask<Root, TChild>(); InsertNavTask<Root, TChild> result = new InsertNavTask<Root, TChild>();
Func<InsertNavProvider<Root, TChild>> func = () => PreFunc().ThenInclude(expression); Func<InsertNavProvider<Root, TChild>> func = () =>
{
var nav = PreFunc().ThenInclude(expression);
nav.NavContext = this.NavContext;
return nav;
};
result.PreFunc = func; result.PreFunc = func;
result.Context = this.Context; result.Context = this.Context;
result.NavContext = this.NavContext;
return result; return result;
} }
public InsertNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, TChild>> expression) where TChild : class, new() public InsertNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, TChild>> expression) where TChild : class, new()
@ -88,6 +110,7 @@ namespace SqlSugar
Func<InsertNavProvider<Root, TChild>> func = () => PreFunc().ThenInclude(expression,options); Func<InsertNavProvider<Root, TChild>> func = () => PreFunc().ThenInclude(expression,options);
result.PreFunc = func; result.PreFunc = func;
result.Context = this.Context; result.Context = this.Context;
result.NavContext = this.NavContext;
return result; return result;
} }
public InsertNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression, InsertNavOptions options) where TChild : class, new() public InsertNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression, InsertNavOptions options) where TChild : class, new()
@ -96,6 +119,7 @@ namespace SqlSugar
Func<InsertNavProvider<Root, TChild>> func = () => PreFunc().ThenInclude(expression, options); Func<InsertNavProvider<Root, TChild>> func = () => PreFunc().ThenInclude(expression, options);
result.PreFunc = func; result.PreFunc = func;
result.Context = this.Context; result.Context = this.Context;
result.NavContext = this.NavContext;
return result; return result;
} }
public InsertNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, TChild>> expression, InsertNavOptions options) where TChild : class, new() public InsertNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, TChild>> expression, InsertNavOptions options) where TChild : class, new()
@ -164,9 +188,16 @@ namespace SqlSugar
private InsertNavTask<Root, Root> AsNav() private InsertNavTask<Root, Root> AsNav()
{ {
InsertNavTask<Root, Root> result = new InsertNavTask<Root, Root>(); InsertNavTask<Root, Root> result = new InsertNavTask<Root, Root>();
Func<InsertNavProvider<Root, Root>> func = () => PreFunc().AsNav(); Func<InsertNavProvider<Root, Root>> func = () => {
var navas= PreFunc().AsNav();
navas.NavContext = this.NavContext;
navas.IsNav = true;
return navas;
};
result.PreFunc = func; result.PreFunc = func;
result.Context = this.Context; result.Context = this.Context;
result.NavContext = this.NavContext;
return result; return result;
} }
} }

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
internal class NavContext
{
public List<NavContextItem> Items { get; set; }
}
internal class NavContextItem
{
public int Level { get; set; }
public string RootName { get; set; }
}
}

View File

@ -1111,6 +1111,7 @@ namespace SqlSugar
provider._Roots = datas; provider._Roots = datas;
provider._Context = this; provider._Context = this;
result.insertNavProvider = provider; result.insertNavProvider = provider;
result.NavContext = new NavContext() { Items = new List<NavContextItem>() };
return result; return result;
} }
public InsertNavTaskInit<T, T> InsertNav<T>(T data, InsertNavRootOptions rootOptions) where T : class, new() public InsertNavTaskInit<T, T> InsertNav<T>(T data, InsertNavRootOptions rootOptions) where T : class, new()
@ -1125,6 +1126,7 @@ namespace SqlSugar
provider._Context = this; provider._Context = this;
provider._RootOptions = rootOptions; provider._RootOptions = rootOptions;
result.insertNavProvider = provider; result.insertNavProvider = provider;
result.NavContext = new NavContext() { Items = new List<NavContextItem>() };
return result; return result;
} }
public DeleteNavTaskInit<T, T> DeleteNav<T>(T data) where T : class, new() public DeleteNavTaskInit<T, T> DeleteNav<T>(T data) where T : class, new()

View File

@ -90,6 +90,7 @@
<Compile Include="Abstract\DeleteProvider\SplitTableDeleteProvider.cs" /> <Compile Include="Abstract\DeleteProvider\SplitTableDeleteProvider.cs" />
<Compile Include="Abstract\EntityMaintenance\EntityColumnExtension.cs" /> <Compile Include="Abstract\EntityMaintenance\EntityColumnExtension.cs" />
<Compile Include="Abstract\EntityMaintenance\EntityMaintenance.cs" /> <Compile Include="Abstract\EntityMaintenance\EntityMaintenance.cs" />
<Compile Include="Abstract\ExecuteNavProvider\NavContext.cs" />
<Compile Include="Abstract\ExecuteNavProvider\UpdateNavOneToOne.cs" /> <Compile Include="Abstract\ExecuteNavProvider\UpdateNavOneToOne.cs" />
<Compile Include="Abstract\ExecuteNavProvider\UpdateNavManyToMany.cs" /> <Compile Include="Abstract\ExecuteNavProvider\UpdateNavManyToMany.cs" />
<Compile Include="Abstract\ExecuteNavProvider\UpdateNavOneToMany.cs" /> <Compile Include="Abstract\ExecuteNavProvider\UpdateNavOneToMany.cs" />