Browse Source

Update insert nav

pull/20/head
sunkaixuan 2 years ago
parent
commit
0b07667d46
  1. 21
      Src/Asp.Net/SqlServerTest/UnitTest/UCustom012.cs
  2. 59
      Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProvider.cs
  3. 95
      Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderHelper.cs
  4. 36
      Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderOneToMany.cs
  5. 34
      Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderOneToOne.cs
  6. 1
      Src/Asp.Net/SqlSugar/SqlSugar.csproj

21
Src/Asp.Net/SqlServerTest/UnitTest/UCustom012.cs

@ -190,25 +190,26 @@ namespace OrmTest
x = SqlFunc.Subqueryable<Order>().Where(z => z.Id == it.Id).Any()
}).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.School_Name=="a").ExecuteCommand();
db.Updateable<StudentA>()
.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.InsertNav(list6)
//.ThenInclude(z1 => z1.SchoolA)
//.ThenInclude(z1 => z1.RoomList)
//.AsNav()
//.ThenInclude(z1 => z1.Books);
db.InsertNav(list6)
.ThenInclude(z1 => z1.SchoolA)
.ThenInclude(z1 => z1.RoomList)
.AsNav()
.ThenInclude(z1 => z1.Books);
}

59
Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProvider.cs

@ -12,7 +12,9 @@ namespace SqlSugar
public List<Root> _Roots { get; set; }
public List<object> _ParentList { get; set; }
public List<object> _RootList { get; set; }
public EntityInfo _ParentEntity { get; set; }
public EntityColumnInfo _ParentPkColumn { get; set; }
public SqlSugarProvider _Context { get; set; }
@ -22,10 +24,10 @@ namespace SqlSugar
_Context = _Context,
_ParentEntity = 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()
{
InitParentList();
@ -49,55 +51,28 @@ namespace SqlSugar
}
return GetResult<TChild>();
}
private static bool IsDefaultValue(object pvValue)
{
return pvValue == null || pvValue == UtilMethods.GetDefaultValue(pvValue.GetType());
}
private void InitParentList()
public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T,List<TChild>>> expression) where TChild : class, new()
{
if (_ParentList == null)
InitParentList();
var name = ExpressionTool.GetMemberName(expression);
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
if (nav.Navigat == null)
{
_ParentList = GetParentList(_Roots).Cast<object>().ToList();
Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性");
}
}
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
};
}
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)
if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
{
InsertIdentity(datas);
InsertOneToOne<TChild>(name, nav);
}
else
else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
{
this._Context.Insertable(datas).ExecuteCommand();
InsertOneToMany<TChild>(name, nav);
}
this._ParentEntity = entity;
result = datas;
return result;
}
private void InsertIdentity<Type>(List<Type> datas) where Type : class, new()
{
foreach (var item in datas)
else
{
this._Context.Insertable(item).ExecuteCommandIdentityIntoEntity();
InsertManyToMany<TChild>(name, nav);
}
return GetResult<TChild>();
}
}
}

95
Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderHelper.cs

@ -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();
}
}
}

36
Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderOneToMany.cs

@ -11,14 +11,46 @@ namespace SqlSugar
private void InsertOneToMany<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
{
List<TChild> children = new List<TChild>();
var parentEntity = _ParentEntity;
var parentList = _ParentList;
var parentColumn = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name);
this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<TChild>();
var parentNavigateProperty = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == name);
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)
{
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;
}
}
}

34
Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderOneToOne.cs

@ -13,9 +13,9 @@ namespace SqlSugar
var parentEntity = _ParentEntity;
var parentList = _ParentList;
var parentColumn = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name);
this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<TChild>();
EntityColumnInfo pkColumn = GetOneTwoOneChildPkColumn(nav);
Check.Exception(pkColumn == null, $" Navigate {parentEntity.EntityName} : {name} is error ", $"导航实体 {parentEntity.EntityName} 属性 {name} 配置错误");
var thisEntity = this._Context.EntityMaintenance.GetEntityInfo<TChild>();
EntityColumnInfo thisPkColumn = GetPkColumnByNav(thisEntity, nav);
Check.Exception(thisPkColumn == null, $" Navigate {parentEntity.EntityName} : {name} is error ", $"导航实体 {parentEntity.EntityName} 属性 {name} 配置错误");
List<TChild> childList = new List<TChild>();
foreach (var parent in parentList)
{
@ -25,38 +25,20 @@ namespace SqlSugar
{
if (IsDefaultValue(navPropertyValue))
{
var pkValue = pkColumn.PropertyInfo.GetValue(childItem);
var pkValue = thisPkColumn.PropertyInfo.GetValue(childItem);
if (IsDefaultValue(navPropertyValue))
{
navPropertyValue = pkValue;
}
}
pkColumn.PropertyInfo.SetValue(childItem, navPropertyValue);
thisPkColumn.PropertyInfo.SetValue(childItem, navPropertyValue);
childList.Add(childItem);
}
}
var x = this._Context.Storageable(childList).WhereColumns(new string[] { pkColumn.PropertyName }).ToStorage();
if (pkColumn.IsIdentity)
{
InsertIdentity(x.InsertList.Select(it => it.Item).ToList());
}
else
{
x.AsInsertable.ExecuteCommand();
}
InsertDatas<TChild>(childList,thisPkColumn);
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;
}
}
}

1
Src/Asp.Net/SqlSugar/SqlSugar.csproj

@ -89,6 +89,7 @@
<Compile Include="Abstract\DeleteProvider\SplitTableDeleteByObjectProvider.cs" />
<Compile Include="Abstract\DeleteProvider\SplitTableDeleteProvider.cs" />
<Compile Include="Abstract\EntityMaintenance\EntityMaintenance.cs" />
<Compile Include="Abstract\ExecuteNavProvider\InsertNavProviderHelper.cs" />
<Compile Include="Abstract\ExecuteNavProvider\InsertNavProviderOneToMany.cs" />
<Compile Include="Abstract\ExecuteNavProvider\InsertNavProviderManyToMany.cs" />
<Compile Include="Abstract\ExecuteNavProvider\InsertNavProviderOneToOne.cs" />

Loading…
Cancel
Save