mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-11-08 18:34:55 +08:00
add navigate insert
This commit is contained in:
@@ -11,6 +11,7 @@ namespace SqlSugar
|
||||
{
|
||||
|
||||
public List<Root> Roots { get; set; }
|
||||
public SqlSugarProvider Context { get; internal set; }
|
||||
|
||||
public DeleteNavProvider<Root,TChild> ThenInclude<TChild>(Expression<Func<T,TChild>> expression)
|
||||
{
|
||||
|
||||
@@ -7,16 +7,78 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class InsertNavProvider<Root,T>
|
||||
public class InsertNavProvider<Root,T> where T : class,new() where Root:class,new()
|
||||
{
|
||||
|
||||
public List<Root> Roots { get; set; }
|
||||
public object ParentList { get; set; }
|
||||
public EntityInfo ParentEntity { get; set; }
|
||||
public SqlSugarProvider Context { get; set; }
|
||||
|
||||
public InsertNavProvider<Root,TChild> ThenInclude<TChild>(Expression<Func<T,TChild>> expression)
|
||||
public InsertNavProvider<Root,TChild> ThenInclude<TChild>(Expression<Func<T,TChild>> expression) where TChild : class, new()
|
||||
{
|
||||
if (ParentList == null)
|
||||
{
|
||||
ParentList = GetParentList(Roots);
|
||||
}
|
||||
var name=ExpressionTool.GetMemberName(expression);
|
||||
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)
|
||||
{
|
||||
InsertOneToOne();
|
||||
}
|
||||
else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
|
||||
{
|
||||
InsertOneToMany();
|
||||
}
|
||||
else
|
||||
{
|
||||
InsertManyToMany();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void InsertManyToMany()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void InsertOneToMany()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void InsertOneToOne()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private List<Type> GetParentList<Type>(List<Type> datas) where Type : class ,new()
|
||||
{
|
||||
List<Type> result = new List<Type>();
|
||||
this.Context.InitMappingInfo<Type>();
|
||||
var entity = this.Context.EntityMaintenance.GetEntityInfo<Type>();
|
||||
var isIdentity = entity.Columns.Where(it=>it.IsIdentity).Any();
|
||||
if (isIdentity)
|
||||
{
|
||||
foreach (var item in datas)
|
||||
{
|
||||
this.Context.Insertable(datas).ExecuteCommandIdentityIntoEntity();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Insertable(datas).ExecuteCommand();
|
||||
}
|
||||
this.ParentEntity = entity;
|
||||
result = datas;
|
||||
return result;
|
||||
}
|
||||
|
||||
public InsertNavProvider<Root,Root> AsNav()
|
||||
{
|
||||
return null;
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace SqlSugar
|
||||
{
|
||||
|
||||
public List<Root> Roots { get; set; }
|
||||
public SqlSugarProvider Context { get; internal set; }
|
||||
|
||||
public UpdateNavProvider<Root,TChild> ThenInclude<TChild>(Expression<Func<T,TChild>> expression)
|
||||
{
|
||||
|
||||
@@ -890,14 +890,15 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region Nav CUD
|
||||
public InsertNavProvider<T, T> InsertNav<T>(T data)
|
||||
public InsertNavProvider<T, T> InsertNav<T>(T data) where T : class, new()
|
||||
{
|
||||
return InsertNav(new List<T>() { data });
|
||||
}
|
||||
public InsertNavProvider<T, T> InsertNav<T>(List<T> datas)
|
||||
public InsertNavProvider<T, T> InsertNav<T>(List<T> datas) where T : class, new()
|
||||
{
|
||||
var result = new InsertNavProvider<T, T>();
|
||||
result.Roots = datas;
|
||||
result.Context = this;
|
||||
return result;
|
||||
}
|
||||
public DeleteNavProvider<T, T> DeleteNav<T>(T data)
|
||||
@@ -908,6 +909,7 @@ namespace SqlSugar
|
||||
{
|
||||
var result = new DeleteNavProvider<T, T>();
|
||||
result.Roots = datas;
|
||||
result.Context = this;
|
||||
return result;
|
||||
}
|
||||
public UpdateNavProvider<T, T> UpdateNav<T>(T data)
|
||||
@@ -918,6 +920,7 @@ namespace SqlSugar
|
||||
{
|
||||
var result = new UpdateNavProvider<T, T>();
|
||||
result.Roots = datas;
|
||||
result.Context = this;
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -678,11 +678,11 @@ namespace SqlSugar
|
||||
{
|
||||
return ScopedContext.Saveable(saveObject);
|
||||
}
|
||||
public InsertNavProvider<T, T> InsertNav<T>(T data)
|
||||
public InsertNavProvider<T, T> InsertNav<T>(T data) where T : class, new()
|
||||
{
|
||||
return ScopedContext.InsertNav(data);
|
||||
}
|
||||
public InsertNavProvider<T, T> InsertNav<T>(List<T> datas)
|
||||
public InsertNavProvider<T, T> InsertNav<T>(List<T> datas) where T : class, new()
|
||||
{
|
||||
return ScopedContext.InsertNav(datas);
|
||||
}
|
||||
|
||||
@@ -203,8 +203,8 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region Nav CUD
|
||||
InsertNavProvider<T, T> InsertNav<T>(T data);
|
||||
InsertNavProvider<T, T> InsertNav<T>(List<T> datas);
|
||||
InsertNavProvider<T, T> InsertNav<T>(T data) where T : class, new();
|
||||
InsertNavProvider<T, T> InsertNav<T>(List<T> datas) where T : class, new();
|
||||
DeleteNavProvider<T, T> DeleteNav<T>(T data);
|
||||
DeleteNavProvider<T, T> DeleteNav<T>(List<T> datas);
|
||||
UpdateNavProvider<T, T> UpdateNav<T>(T data);
|
||||
|
||||
@@ -154,11 +154,11 @@ namespace SqlSugar
|
||||
#region Queryable
|
||||
|
||||
#region Nav CUD
|
||||
public InsertNavProvider<T, T> InsertNav<T>(T data)
|
||||
public InsertNavProvider<T, T> InsertNav<T>(T data) where T : class, new()
|
||||
{
|
||||
return this.Context.InsertNav(data);
|
||||
}
|
||||
public InsertNavProvider<T, T> InsertNav<T>(List<T> datas)
|
||||
public InsertNavProvider<T, T> InsertNav<T>(List<T> datas) where T : class, new()
|
||||
{
|
||||
return this.Context.InsertNav(datas);
|
||||
}
|
||||
|
||||
@@ -730,11 +730,11 @@ namespace SqlSugar
|
||||
return ScopedContext.DeleteableWithAttr<T>(deleteObjs);
|
||||
}
|
||||
|
||||
public InsertNavProvider<T, T> InsertNav<T>(T data)
|
||||
public InsertNavProvider<T, T> InsertNav<T>(T data) where T : class, new()
|
||||
{
|
||||
return ScopedContext.InsertNav(data);
|
||||
}
|
||||
public InsertNavProvider<T, T> InsertNav<T>(List<T> datas)
|
||||
public InsertNavProvider<T, T> InsertNav<T>(List<T> datas) where T : class, new()
|
||||
{
|
||||
return ScopedContext.InsertNav(datas);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user