mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-05-03 20:27:56 +08:00
Update insert nav
This commit is contained in:
parent
346a2595c3
commit
0b07667d46
@ -190,25 +190,26 @@ namespace OrmTest
|
|||||||
x = SqlFunc.Subqueryable<Order>().Where(z => z.Id == it.Id).Any()
|
x = SqlFunc.Subqueryable<Order>().Where(z => z.Id == it.Id).Any()
|
||||||
}).ToList();
|
}).ToList();
|
||||||
|
|
||||||
|
var list6 = db.Queryable<StudentA>()
|
||||||
|
.Includes(x => x.SchoolA, x => x.RoomList)
|
||||||
|
.Includes(x => x.Books).ToList();
|
||||||
|
|
||||||
db.Deleteable<StudentA>().Where(x => x.SchoolA.TeacherList.Any()).ExecuteCommand();
|
db.Deleteable<StudentA>().Where(x => x.SchoolA.TeacherList.Any()).ExecuteCommand();
|
||||||
db.Deleteable<StudentA>().Where(x => x.SchoolA.School_Name=="a").ExecuteCommand();
|
db.Deleteable<StudentA>().Where(x => x.SchoolA.School_Name=="a").ExecuteCommand();
|
||||||
db.Updateable<StudentA>()
|
db.Updateable<StudentA>()
|
||||||
.SetColumns(it=>it.Name=="a").Where(x => x.SchoolA.School_Name == "a").ExecuteCommand();
|
.SetColumns(it=>it.Name=="a").Where(x => x.SchoolA.School_Name == "a").ExecuteCommand();
|
||||||
|
|
||||||
|
|
||||||
var list6=db.Queryable<StudentA>()
|
|
||||||
.Includes(x => x.SchoolA, x => x.RoomList)
|
|
||||||
.Includes(x => x.Books).ToList();
|
|
||||||
|
|
||||||
|
|
||||||
db.DbMaintenance.TruncateTable<StudentA, RoomA, BookA>();
|
db.DbMaintenance.TruncateTable<StudentA, RoomA, BookA>();
|
||||||
|
|
||||||
//开发中
|
//开发中
|
||||||
//db.InsertNav(list6)
|
db.InsertNav(list6)
|
||||||
//.ThenInclude(z1 => z1.SchoolA)
|
.ThenInclude(z1 => z1.SchoolA)
|
||||||
//.ThenInclude(z1 => z1.RoomList)
|
.ThenInclude(z1 => z1.RoomList)
|
||||||
//.AsNav()
|
.AsNav()
|
||||||
//.ThenInclude(z1 => z1.Books);
|
.ThenInclude(z1 => z1.Books);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,9 @@ namespace SqlSugar
|
|||||||
|
|
||||||
public List<Root> _Roots { get; set; }
|
public List<Root> _Roots { get; set; }
|
||||||
public List<object> _ParentList { get; set; }
|
public List<object> _ParentList { get; set; }
|
||||||
|
public List<object> _RootList { get; set; }
|
||||||
public EntityInfo _ParentEntity { get; set; }
|
public EntityInfo _ParentEntity { get; set; }
|
||||||
|
public EntityColumnInfo _ParentPkColumn { get; set; }
|
||||||
public SqlSugarProvider _Context { get; set; }
|
public SqlSugarProvider _Context { get; set; }
|
||||||
|
|
||||||
|
|
||||||
@ -22,10 +24,10 @@ namespace SqlSugar
|
|||||||
_Context = _Context,
|
_Context = _Context,
|
||||||
_ParentEntity = null,
|
_ParentEntity = null,
|
||||||
_ParentList=null,
|
_ParentList=null,
|
||||||
_Roots= _Roots
|
_Roots= _Roots ,
|
||||||
|
_ParentPkColumn=this._Context.EntityMaintenance.GetEntityInfo<Root>().Columns.First(it=>it.IsPrimarykey)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
|
public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
|
||||||
{
|
{
|
||||||
InitParentList();
|
InitParentList();
|
||||||
@ -49,55 +51,28 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
return GetResult<TChild>();
|
return GetResult<TChild>();
|
||||||
}
|
}
|
||||||
|
public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T,List<TChild>>> expression) where TChild : class, new()
|
||||||
private static bool IsDefaultValue(object pvValue)
|
|
||||||
{
|
{
|
||||||
return pvValue == null || pvValue == UtilMethods.GetDefaultValue(pvValue.GetType());
|
InitParentList();
|
||||||
}
|
var name = ExpressionTool.GetMemberName(expression);
|
||||||
private void InitParentList()
|
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
|
||||||
{
|
if (nav.Navigat == null)
|
||||||
if (_ParentList == null)
|
|
||||||
{
|
{
|
||||||
_ParentList = GetParentList(_Roots).Cast<object>().ToList();
|
Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性");
|
||||||
}
|
}
|
||||||
}
|
if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
|
||||||
|
|
||||||
private InsertNavProvider<Root, TChild> GetResult<TChild>() where TChild : class, new()
|
|
||||||
{
|
|
||||||
return new InsertNavProvider<Root, TChild>()
|
|
||||||
{
|
{
|
||||||
_Context = this._Context,
|
InsertOneToOne<TChild>(name, nav);
|
||||||
_ParentEntity = this._ParentEntity,
|
|
||||||
_ParentList = this._ParentList,
|
|
||||||
_Roots = this._Roots
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
InsertIdentity(datas);
|
|
||||||
}
|
}
|
||||||
else
|
else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
|
||||||
{
|
{
|
||||||
this._Context.Insertable(datas).ExecuteCommand();
|
InsertOneToMany<TChild>(name, nav);
|
||||||
}
|
}
|
||||||
this._ParentEntity = entity;
|
else
|
||||||
result = datas;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InsertIdentity<Type>(List<Type> datas) where Type : class, new()
|
|
||||||
{
|
|
||||||
foreach (var item in datas)
|
|
||||||
{
|
{
|
||||||
this._Context.Insertable(item).ExecuteCommandIdentityIntoEntity();
|
InsertManyToMany<TChild>(name, nav);
|
||||||
}
|
}
|
||||||
|
return GetResult<TChild>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,95 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SqlSugar
|
||||||
|
{
|
||||||
|
public partial class InsertNavProvider<Root,T> where T : class,new() where Root:class,new()
|
||||||
|
{
|
||||||
|
|
||||||
|
private static bool IsDefaultValue(object pvValue)
|
||||||
|
{
|
||||||
|
return pvValue == null || pvValue == UtilMethods.GetDefaultValue(pvValue.GetType());
|
||||||
|
}
|
||||||
|
private void InitParentList()
|
||||||
|
{
|
||||||
|
if (_RootList == null)
|
||||||
|
{
|
||||||
|
_RootList= _ParentList = GetRootList(_Roots).Cast<object>().ToList();
|
||||||
|
}
|
||||||
|
else if (_ParentList == null)
|
||||||
|
{
|
||||||
|
_ParentList = _RootList;
|
||||||
|
var pkColumn = this._Context.EntityMaintenance.GetEntityInfo<T>().Columns.FirstOrDefault(it => it.IsPrimarykey);
|
||||||
|
this._ParentPkColumn = pkColumn;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private InsertNavProvider<Root, TChild> GetResult<TChild>() where TChild : class, new()
|
||||||
|
{
|
||||||
|
return new InsertNavProvider<Root, TChild>()
|
||||||
|
{
|
||||||
|
_Context = this._Context,
|
||||||
|
_ParentEntity = this._ParentEntity,
|
||||||
|
_ParentList = this._ParentList,
|
||||||
|
_Roots = this._Roots,
|
||||||
|
_ParentPkColumn=this._ParentPkColumn,
|
||||||
|
_RootList=this._RootList
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Type> GetRootList<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 pkColumn = entity.Columns.FirstOrDefault(it => it.IsPrimarykey);
|
||||||
|
InsertDatas(datas, pkColumn);
|
||||||
|
this._ParentEntity = entity;
|
||||||
|
result = datas;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void InsertIdentity<Type>(List<Type> datas) where Type : class, new()
|
||||||
|
{
|
||||||
|
foreach (var item in datas)
|
||||||
|
{
|
||||||
|
this._Context.Insertable(item).ExecuteCommandIdentityIntoEntity();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private EntityColumnInfo GetPkColumnByNav(EntityInfo entity,EntityColumnInfo nav)
|
||||||
|
{
|
||||||
|
var pkColumn = entity.Columns.FirstOrDefault(it => it.IsPrimarykey == true);
|
||||||
|
if (nav.Navigat.Name2.HasValue())
|
||||||
|
{
|
||||||
|
pkColumn = _ParentEntity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name2);
|
||||||
|
}
|
||||||
|
return pkColumn;
|
||||||
|
}
|
||||||
|
private EntityColumnInfo GetFKColumnByNav(EntityInfo entity, EntityColumnInfo nav)
|
||||||
|
{
|
||||||
|
var fkColumn = entity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name);
|
||||||
|
return fkColumn;
|
||||||
|
}
|
||||||
|
private void InsertDatas<TChild>(List<TChild> children, EntityColumnInfo pkColumn) where TChild : class, new()
|
||||||
|
{
|
||||||
|
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 (pkColumn.IsIdentity)
|
||||||
|
{
|
||||||
|
InsertIdentity(insertData);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this._Context.Insertable(insertData).ExecuteCommand();
|
||||||
|
}
|
||||||
|
this._ParentList = children.Cast<object>().ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -11,14 +11,46 @@ namespace SqlSugar
|
|||||||
|
|
||||||
private void InsertOneToMany<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
|
private void InsertOneToMany<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
|
||||||
{
|
{
|
||||||
|
List<TChild> children = new List<TChild>();
|
||||||
var parentEntity = _ParentEntity;
|
var parentEntity = _ParentEntity;
|
||||||
var parentList = _ParentList;
|
var parentList = _ParentList;
|
||||||
var parentColumn = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name);
|
var parentNavigateProperty = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == name);
|
||||||
this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<TChild>();
|
var thisEntity = this._Context.EntityMaintenance.GetEntityInfo<TChild>();
|
||||||
|
var thisPkColumn = GetPkColumnByNav(thisEntity, nav);
|
||||||
|
var thisFkColumn= GetFKColumnByNav(thisEntity, nav);
|
||||||
|
EntityColumnInfo parentPkColumn = GetParentPkColumn();
|
||||||
foreach (var item in parentList)
|
foreach (var item in parentList)
|
||||||
{
|
{
|
||||||
|
var parentValue = parentPkColumn.PropertyInfo.GetValue(item);
|
||||||
|
var childs = parentNavigateProperty.PropertyInfo.GetValue(item) as List<TChild>;
|
||||||
|
if (childs != null)
|
||||||
|
{
|
||||||
|
foreach (var child in childs)
|
||||||
|
{
|
||||||
|
thisFkColumn.PropertyInfo.SetValue(child, parentValue, null);
|
||||||
|
}
|
||||||
|
children.AddRange(childs);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
InsertDatas(children, thisPkColumn);
|
||||||
|
SetNewParent<TChild>(thisEntity,thisPkColumn);
|
||||||
|
}
|
||||||
|
|
||||||
|
private EntityColumnInfo GetParentPkColumn()
|
||||||
|
{
|
||||||
|
EntityColumnInfo parentPkColumn = _ParentPkColumn;
|
||||||
|
if (_ParentPkColumn == null)
|
||||||
|
{
|
||||||
|
_ParentPkColumn= this._ParentEntity.Columns.FirstOrDefault(it => it.IsPrimarykey);
|
||||||
|
}
|
||||||
|
|
||||||
|
return parentPkColumn;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetNewParent<TChild>(EntityInfo entityInfo,EntityColumnInfo entityColumnInfo) where TChild : class, new()
|
||||||
|
{
|
||||||
|
this._ParentEntity = entityInfo;
|
||||||
|
this._ParentPkColumn = entityColumnInfo;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,9 +13,9 @@ namespace SqlSugar
|
|||||||
var parentEntity = _ParentEntity;
|
var parentEntity = _ParentEntity;
|
||||||
var parentList = _ParentList;
|
var parentList = _ParentList;
|
||||||
var parentColumn = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name);
|
var parentColumn = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name);
|
||||||
this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<TChild>();
|
var thisEntity = this._Context.EntityMaintenance.GetEntityInfo<TChild>();
|
||||||
EntityColumnInfo pkColumn = GetOneTwoOneChildPkColumn(nav);
|
EntityColumnInfo thisPkColumn = GetPkColumnByNav(thisEntity, nav);
|
||||||
Check.Exception(pkColumn == null, $" Navigate {parentEntity.EntityName} : {name} is error ", $"导航实体 {parentEntity.EntityName} 属性 {name} 配置错误");
|
Check.Exception(thisPkColumn == null, $" Navigate {parentEntity.EntityName} : {name} is error ", $"导航实体 {parentEntity.EntityName} 属性 {name} 配置错误");
|
||||||
List<TChild> childList = new List<TChild>();
|
List<TChild> childList = new List<TChild>();
|
||||||
foreach (var parent in parentList)
|
foreach (var parent in parentList)
|
||||||
{
|
{
|
||||||
@ -25,38 +25,20 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
if (IsDefaultValue(navPropertyValue))
|
if (IsDefaultValue(navPropertyValue))
|
||||||
{
|
{
|
||||||
var pkValue = pkColumn.PropertyInfo.GetValue(childItem);
|
var pkValue = thisPkColumn.PropertyInfo.GetValue(childItem);
|
||||||
if (IsDefaultValue(navPropertyValue))
|
if (IsDefaultValue(navPropertyValue))
|
||||||
{
|
{
|
||||||
navPropertyValue = pkValue;
|
navPropertyValue = pkValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pkColumn.PropertyInfo.SetValue(childItem, navPropertyValue);
|
thisPkColumn.PropertyInfo.SetValue(childItem, navPropertyValue);
|
||||||
childList.Add(childItem);
|
childList.Add(childItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var x = this._Context.Storageable(childList).WhereColumns(new string[] { pkColumn.PropertyName }).ToStorage();
|
InsertDatas<TChild>(childList,thisPkColumn);
|
||||||
if (pkColumn.IsIdentity)
|
|
||||||
{
|
|
||||||
InsertIdentity(x.InsertList.Select(it => it.Item).ToList());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
x.AsInsertable.ExecuteCommand();
|
|
||||||
}
|
|
||||||
this._ParentList = childList.Cast<object>().ToList();
|
this._ParentList = childList.Cast<object>().ToList();
|
||||||
|
SetNewParent<TChild>(thisEntity, thisPkColumn);
|
||||||
}
|
}
|
||||||
|
|
||||||
private EntityColumnInfo GetOneTwoOneChildPkColumn(EntityColumnInfo nav)
|
|
||||||
{
|
|
||||||
var pkColumn = _ParentEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true);
|
|
||||||
if (nav.Navigat.Name2.HasValue())
|
|
||||||
{
|
|
||||||
pkColumn = _ParentEntity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name2);
|
|
||||||
}
|
|
||||||
|
|
||||||
return pkColumn;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,6 +89,7 @@
|
|||||||
<Compile Include="Abstract\DeleteProvider\SplitTableDeleteByObjectProvider.cs" />
|
<Compile Include="Abstract\DeleteProvider\SplitTableDeleteByObjectProvider.cs" />
|
||||||
<Compile Include="Abstract\DeleteProvider\SplitTableDeleteProvider.cs" />
|
<Compile Include="Abstract\DeleteProvider\SplitTableDeleteProvider.cs" />
|
||||||
<Compile Include="Abstract\EntityMaintenance\EntityMaintenance.cs" />
|
<Compile Include="Abstract\EntityMaintenance\EntityMaintenance.cs" />
|
||||||
|
<Compile Include="Abstract\ExecuteNavProvider\InsertNavProviderHelper.cs" />
|
||||||
<Compile Include="Abstract\ExecuteNavProvider\InsertNavProviderOneToMany.cs" />
|
<Compile Include="Abstract\ExecuteNavProvider\InsertNavProviderOneToMany.cs" />
|
||||||
<Compile Include="Abstract\ExecuteNavProvider\InsertNavProviderManyToMany.cs" />
|
<Compile Include="Abstract\ExecuteNavProvider\InsertNavProviderManyToMany.cs" />
|
||||||
<Compile Include="Abstract\ExecuteNavProvider\InsertNavProviderOneToOne.cs" />
|
<Compile Include="Abstract\ExecuteNavProvider\InsertNavProviderOneToOne.cs" />
|
||||||
|
Loading…
Reference in New Issue
Block a user