mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Add db.UpdateNav(list)
This commit is contained in:
parent
b411e9deef
commit
69080be4a6
@ -1,41 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace SqlSugar
|
|
||||||
{
|
|
||||||
public class UpdateNavProvider<Root,T>
|
|
||||||
{
|
|
||||||
|
|
||||||
public List<Root> Roots { get; set; }
|
|
||||||
public SqlSugarProvider Context { get; internal set; }
|
|
||||||
|
|
||||||
public UpdateNavProvider<Root,TChild> ThenInclude<TChild>(Expression<Func<T,TChild>> expression)
|
|
||||||
{
|
|
||||||
throw new Exception("开发中7月15号之前上线");
|
|
||||||
}
|
|
||||||
public UpdateNavProvider<Root, TChild> Include<TChild>(Expression<Func<T, TChild>> expression)
|
|
||||||
{
|
|
||||||
throw new Exception("开发中7月15号之前上线");
|
|
||||||
}
|
|
||||||
public UpdateNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression)
|
|
||||||
{
|
|
||||||
throw new Exception("开发中7月15号之前上线");
|
|
||||||
}
|
|
||||||
public UpdateNavProvider<Root, TChild> Include<TChild>(Expression<Func<T, List<TChild>>> expression)
|
|
||||||
{
|
|
||||||
throw new Exception("开发中7月15号之前上线");
|
|
||||||
}
|
|
||||||
public bool ExecuteCommand()
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
private UpdateNavProvider<Root,Root> AsNav()
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,78 @@
|
|||||||
|
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 UpdateNavProvider<Root, T> where T : class, new() where Root : class, new()
|
||||||
|
{
|
||||||
|
private void UpdateManyToMany<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
|
||||||
|
{
|
||||||
|
;
|
||||||
|
var parentEntity = _ParentEntity;
|
||||||
|
var parentList = _ParentList;
|
||||||
|
var parentPkColumn = parentEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true);
|
||||||
|
var parentNavigateProperty = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == name);
|
||||||
|
var thisEntity = this._Context.EntityMaintenance.GetEntityInfo<TChild>();
|
||||||
|
var thisPkColumn = thisEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true);
|
||||||
|
Check.Exception(thisPkColumn == null, $"{thisPkColumn.EntityName} need primary key", $"{thisPkColumn.EntityName}需要主键");
|
||||||
|
Check.Exception(parentPkColumn == null, $"{parentPkColumn.EntityName} need primary key", $"{parentPkColumn.EntityName}需要主键");
|
||||||
|
var mappingType = parentNavigateProperty.Navigat.MappingType;
|
||||||
|
var mappingEntity = this._Context.EntityMaintenance.GetEntityInfo(mappingType);
|
||||||
|
var mappingA = mappingEntity.Columns.FirstOrDefault(x => x.PropertyName == parentNavigateProperty.Navigat.MappingAId);
|
||||||
|
var mappingB = mappingEntity.Columns.FirstOrDefault(x => x.PropertyName == parentNavigateProperty.Navigat.MappingBId);
|
||||||
|
var mappingPk = mappingEntity.Columns
|
||||||
|
.Where(it => it.PropertyName != mappingA.PropertyName)
|
||||||
|
.Where(it => it.PropertyName != mappingB.PropertyName)
|
||||||
|
.Where(it => it.IsPrimarykey && !it.IsIdentity && it.OracleSequenceName.IsNullOrEmpty()).FirstOrDefault();
|
||||||
|
Check.Exception(mappingA == null || mappingB == null, $"Navigate property {name} error ", $"导航属性{name}配置错误");
|
||||||
|
List<Dictionary<string, object>> mappgingTables = new List<Dictionary<string, object>>();
|
||||||
|
foreach (var item in parentList)
|
||||||
|
{
|
||||||
|
var items = parentNavigateProperty.PropertyInfo.GetValue(item);
|
||||||
|
var children = ((List<TChild>)items);
|
||||||
|
InsertDatas(children, thisPkColumn);
|
||||||
|
var parentId = parentPkColumn.PropertyInfo.GetValue(item);
|
||||||
|
foreach (var child in children)
|
||||||
|
{
|
||||||
|
var chidId = thisPkColumn.PropertyInfo.GetValue(child);
|
||||||
|
Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
|
||||||
|
keyValuePairs.Add(mappingA.DbColumnName, parentId);
|
||||||
|
keyValuePairs.Add(mappingB.DbColumnName, chidId);
|
||||||
|
if (mappingPk != null)
|
||||||
|
{
|
||||||
|
SetMappingTableDefaultValue(mappingPk, keyValuePairs);
|
||||||
|
}
|
||||||
|
mappgingTables.Add(keyValuePairs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var ids = mappgingTables.Select(x => x[mappingA.DbColumnName]).ToList();
|
||||||
|
this._Context.Deleteable<object>().AS(mappingEntity.DbTableName).In(mappingA.DbColumnName, ids).ExecuteCommand();
|
||||||
|
this._Context.Insertable(mappgingTables).AS(mappingEntity.DbTableName).ExecuteCommand();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetMappingTableDefaultValue(EntityColumnInfo mappingPk, Dictionary<string, object> keyValuePairs)
|
||||||
|
{
|
||||||
|
if (mappingPk.UnderType == UtilConstants.LongType)
|
||||||
|
{
|
||||||
|
keyValuePairs.Add(mappingPk.DbColumnName, SnowFlakeSingle.Instance.NextId());
|
||||||
|
}
|
||||||
|
else if (mappingPk.UnderType == UtilConstants.GuidType)
|
||||||
|
{
|
||||||
|
keyValuePairs.Add(mappingPk.DbColumnName, Guid.NewGuid());
|
||||||
|
}
|
||||||
|
else if (mappingPk.UnderType == UtilConstants.StringType)
|
||||||
|
{
|
||||||
|
keyValuePairs.Add(mappingPk.DbColumnName, Guid.NewGuid() + "");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var name = mappingPk.EntityName + " " + mappingPk.DbColumnName;
|
||||||
|
Check.ExceptionEasy($"The field {name} is not an autoassignment type and requires an assignment",
|
||||||
|
$" 中间表主键字段{name}不是可自动赋值类型, 可赋值类型有 自增、long、Guid、string。你也可以删掉主键 用双主键");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
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 UpdateNavProvider<Root, T> where T : class, new() where Root : class, new()
|
||||||
|
{
|
||||||
|
|
||||||
|
private void UpdateOneToMany<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
|
||||||
|
{
|
||||||
|
List<TChild> children = new List<TChild>();
|
||||||
|
var parentEntity = _ParentEntity;
|
||||||
|
var parentList = _ParentList;
|
||||||
|
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();
|
||||||
|
EntityColumnInfo parentNavColumn = GetParentPkNavColumn(nav);
|
||||||
|
if (parentNavColumn != null)
|
||||||
|
{
|
||||||
|
parentPkColumn = parentNavColumn;
|
||||||
|
}
|
||||||
|
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 = _ParentPkColumn = this._ParentEntity.Columns.FirstOrDefault(it => it.IsPrimarykey);
|
||||||
|
}
|
||||||
|
return parentPkColumn;
|
||||||
|
}
|
||||||
|
private EntityColumnInfo GetParentPkNavColumn(EntityColumnInfo nav)
|
||||||
|
{
|
||||||
|
EntityColumnInfo result = null;
|
||||||
|
if (nav.Navigat.Name2.HasValue())
|
||||||
|
{
|
||||||
|
result = _ParentPkColumn = this._ParentEntity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name2);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetNewParent<TChild>(EntityInfo entityInfo, EntityColumnInfo entityColumnInfo) where TChild : class, new()
|
||||||
|
{
|
||||||
|
this._ParentEntity = entityInfo;
|
||||||
|
this._ParentPkColumn = entityColumnInfo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
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 UpdateNavProvider<Root, T> where T : class, new() where Root : class, new()
|
||||||
|
{
|
||||||
|
private void UpdateOneToOne<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
|
||||||
|
{
|
||||||
|
var parentEntity = _ParentEntity;
|
||||||
|
var parentList = _ParentList;
|
||||||
|
var parentColumn = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name);
|
||||||
|
var parentPkColumn = parentEntity.Columns.FirstOrDefault(it => it.IsPrimarykey);
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
var navPropertyValue = parentColumn.PropertyInfo.GetValue(parent);
|
||||||
|
var childItem = (TChild)nav.PropertyInfo.GetValue(parent);
|
||||||
|
if (childItem != null)
|
||||||
|
{
|
||||||
|
if (IsDefaultValue(navPropertyValue))
|
||||||
|
{
|
||||||
|
var pkValue = thisPkColumn.PropertyInfo.GetValue(childItem);
|
||||||
|
if (IsDefaultValue(navPropertyValue))
|
||||||
|
{
|
||||||
|
navPropertyValue = pkValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if (!IsDefaultValue(navPropertyValue))
|
||||||
|
{
|
||||||
|
this._Context.Updateable<DbTableInfo>
|
||||||
|
().AS(parentEntity.DbTableName)
|
||||||
|
.SetColumns(parentColumn.DbColumnName, navPropertyValue)
|
||||||
|
.Where(parentPkColumn.DbColumnName, "=", parentPkColumn.PropertyInfo.GetValue(parent)).ExecuteCommand();
|
||||||
|
}
|
||||||
|
if (IsDefaultValue(navPropertyValue))
|
||||||
|
{
|
||||||
|
InsertDatas<TChild>(new List<TChild>() { childItem }, thisPkColumn);
|
||||||
|
navPropertyValue = thisPkColumn.PropertyInfo.GetValue(childItem);
|
||||||
|
parentColumn.PropertyInfo.SetValue(parent, navPropertyValue);
|
||||||
|
this._Context.Updateable<DbTableInfo>
|
||||||
|
().AS(parentEntity.DbTableName)
|
||||||
|
.SetColumns(parentColumn.DbColumnName, navPropertyValue)
|
||||||
|
.Where(parentPkColumn.DbColumnName, "=", parentPkColumn.PropertyInfo.GetValue(parent)).ExecuteCommand();
|
||||||
|
}
|
||||||
|
|
||||||
|
thisPkColumn.PropertyInfo.SetValue(childItem, navPropertyValue);
|
||||||
|
childList.Add(childItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
InsertDatas<TChild>(childList, thisPkColumn);
|
||||||
|
this._ParentList = childList.Cast<object>().ToList();
|
||||||
|
SetNewParent<TChild>(thisEntity, thisPkColumn);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,79 @@
|
|||||||
|
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 UpdateNavProvider<Root, T> where T : class, new() where Root : class, new()
|
||||||
|
{
|
||||||
|
|
||||||
|
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; }
|
||||||
|
|
||||||
|
|
||||||
|
public UpdateNavProvider<Root, Root> AsNav()
|
||||||
|
{
|
||||||
|
return new UpdateNavProvider<Root, Root>
|
||||||
|
{
|
||||||
|
_Context = _Context,
|
||||||
|
_ParentEntity = null,
|
||||||
|
_ParentList = null,
|
||||||
|
_Roots = _Roots,
|
||||||
|
_ParentPkColumn = this._Context.EntityMaintenance.GetEntityInfo<Root>().Columns.First(it => it.IsPrimarykey)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
public UpdateNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
|
||||||
|
{
|
||||||
|
InitParentList();
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
UpdateOneToOne<TChild>(name, nav);
|
||||||
|
}
|
||||||
|
else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
|
||||||
|
{
|
||||||
|
UpdateOneToMany<TChild>(name, nav);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
UpdateManyToMany<TChild>(name, nav);
|
||||||
|
}
|
||||||
|
return GetResult<TChild>();
|
||||||
|
}
|
||||||
|
public UpdateNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new()
|
||||||
|
{
|
||||||
|
InitParentList();
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
UpdateOneToOne<TChild>(name, nav);
|
||||||
|
}
|
||||||
|
else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
|
||||||
|
{
|
||||||
|
UpdateOneToMany<TChild>(name, nav);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
UpdateManyToMany<TChild>(name, nav);
|
||||||
|
}
|
||||||
|
return GetResult<TChild>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,137 @@
|
|||||||
|
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 UpdateNavProvider<Root, T> where T : class, new() where Root : class, new()
|
||||||
|
{
|
||||||
|
|
||||||
|
private static bool IsDefaultValue(object pvValue)
|
||||||
|
{
|
||||||
|
return pvValue == null || pvValue.Equals(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 UpdateNavProvider<Root, TChild> GetResult<TChild>() where TChild : class, new()
|
||||||
|
{
|
||||||
|
return new UpdateNavProvider<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 = entity.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, EntityColumnInfo NavColumn = null) where TChild : class, new()
|
||||||
|
{
|
||||||
|
children = children.Distinct().ToList();
|
||||||
|
var x = this._Context.Storageable(children).WhereColumns(new string[] { pkColumn.PropertyName }).ToStorage();
|
||||||
|
var UpdateData = children = x.UpdateList.Select(it => it.Item).ToList();
|
||||||
|
Check.ExceptionEasy(pkColumn == null && NavColumn == null, $"The entity is invalid", $"实体错误无法使用导航");
|
||||||
|
InitData(pkColumn, UpdateData);
|
||||||
|
this._ParentList = children.Cast<object>().ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void InitData<TChild>(EntityColumnInfo pkColumn, List<TChild> UpdateData) where TChild : class, new()
|
||||||
|
{
|
||||||
|
if (pkColumn.IsIdentity || pkColumn.OracleSequenceName.HasValue())
|
||||||
|
{
|
||||||
|
InsertIdentity(UpdateData);
|
||||||
|
}
|
||||||
|
else if (pkColumn.UnderType == UtilConstants.LongType)
|
||||||
|
{
|
||||||
|
SetValue(pkColumn, UpdateData, () => SnowFlakeSingle.Instance.NextId());
|
||||||
|
}
|
||||||
|
else if (pkColumn.UnderType == UtilConstants.GuidType)
|
||||||
|
{
|
||||||
|
SetValue(pkColumn, UpdateData, () => Guid.NewGuid());
|
||||||
|
}
|
||||||
|
else if (pkColumn.UnderType == UtilConstants.StringType)
|
||||||
|
{
|
||||||
|
SetValue(pkColumn, UpdateData, () => Guid.NewGuid().ToString());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
SetError(pkColumn, UpdateData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetValue<TChild>(EntityColumnInfo pkColumn, List<TChild> UpdateData, Func<object> value) where TChild : class, new()
|
||||||
|
{
|
||||||
|
foreach (var child in UpdateData)
|
||||||
|
{
|
||||||
|
if (IsDefaultValue(pkColumn.PropertyInfo.GetValue(child)))
|
||||||
|
{
|
||||||
|
pkColumn.PropertyInfo.SetValue(child, value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this._Context.Updateable(UpdateData).ExecuteCommand();
|
||||||
|
}
|
||||||
|
private void SetError<TChild>(EntityColumnInfo pkColumn, List<TChild> UpdateData) where TChild : class, new()
|
||||||
|
{
|
||||||
|
foreach (var child in UpdateData)
|
||||||
|
{
|
||||||
|
if (IsDefaultValue(pkColumn.PropertyInfo.GetValue(child)))
|
||||||
|
{
|
||||||
|
var name = pkColumn.EntityName + " " + pkColumn.DbColumnName;
|
||||||
|
Check.ExceptionEasy($"The field {name} is not an autoassignment type and requires an assignment", $"字段{name}不是可自动赋值类型,需要赋值 , 可赋值类型有 自增、long、Guid、string");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this._Context.Updateable(UpdateData).ExecuteCommand();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,99 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SqlSugar
|
||||||
|
{
|
||||||
|
public class UpdateNavTaskInit<Root, T> where T : class, new() where Root : class, new()
|
||||||
|
{
|
||||||
|
|
||||||
|
internal SqlSugarProvider Context { get; set; }
|
||||||
|
internal UpdateNavProvider<Root, Root> UpdateNavProvider { get; set; }
|
||||||
|
|
||||||
|
public UpdateNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, TChild>> expression) where TChild : class, new()
|
||||||
|
{
|
||||||
|
this.Context = UpdateNavProvider._Context;
|
||||||
|
UpdateNavTask<Root, TChild> result = new UpdateNavTask<Root, TChild>();
|
||||||
|
Func<UpdateNavProvider<Root, TChild>> func = () => UpdateNavProvider.ThenInclude(expression);
|
||||||
|
result.PreFunc = func;
|
||||||
|
result.Context = this.Context;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
public UpdateNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, List<TChild>>> expression) where TChild : class, new()
|
||||||
|
{
|
||||||
|
this.Context = UpdateNavProvider._Context;
|
||||||
|
UpdateNavTask<Root, TChild> result = new UpdateNavTask<Root, TChild>();
|
||||||
|
Func<UpdateNavProvider<Root, TChild>> func = () => UpdateNavProvider.ThenInclude(expression);
|
||||||
|
result.PreFunc = func;
|
||||||
|
result.Context = this.Context;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class UpdateNavTask<Root, T> where T : class, new() where Root : class, new()
|
||||||
|
{
|
||||||
|
public SqlSugarProvider Context { get; set; }
|
||||||
|
public Func<UpdateNavProvider<Root, T>> PreFunc { get; set; }
|
||||||
|
public UpdateNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
|
||||||
|
{
|
||||||
|
UpdateNavTask<Root, TChild> result = new UpdateNavTask<Root, TChild>();
|
||||||
|
Func<UpdateNavProvider<Root, TChild>> func = () => PreFunc().ThenInclude(expression);
|
||||||
|
result.PreFunc = func;
|
||||||
|
result.Context = this.Context;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
public UpdateNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new()
|
||||||
|
{
|
||||||
|
UpdateNavTask<Root, TChild> result = new UpdateNavTask<Root, TChild>();
|
||||||
|
Func<UpdateNavProvider<Root, TChild>> func = () => PreFunc().ThenInclude(expression);
|
||||||
|
result.PreFunc = func;
|
||||||
|
result.Context = this.Context;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
public UpdateNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, TChild>> expression) where TChild : class, new()
|
||||||
|
{
|
||||||
|
return AsNav().ThenInclude(expression);
|
||||||
|
}
|
||||||
|
public UpdateNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, List<TChild>>> expression) where TChild : class, new()
|
||||||
|
{
|
||||||
|
return AsNav().ThenInclude(expression);
|
||||||
|
}
|
||||||
|
public bool ExecuteCommand()
|
||||||
|
{
|
||||||
|
var hasTran = this.Context.Ado.Transaction != null;
|
||||||
|
if (hasTran)
|
||||||
|
{
|
||||||
|
PreFunc();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.Context.Ado.UseTran(() =>
|
||||||
|
{
|
||||||
|
PreFunc();
|
||||||
|
}, ex => throw ex);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public async Task<bool> ExecuteCommandAsync()
|
||||||
|
{
|
||||||
|
await Task.Run(async () =>
|
||||||
|
{
|
||||||
|
ExecuteCommand();
|
||||||
|
await Task.Delay(0);
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private UpdateNavTask<Root, Root> AsNav()
|
||||||
|
{
|
||||||
|
UpdateNavTask<Root, Root> result = new UpdateNavTask<Root, Root>();
|
||||||
|
Func<UpdateNavProvider<Root, Root>> func = () => PreFunc().AsNav();
|
||||||
|
result.PreFunc = func;
|
||||||
|
result.Context = this.Context;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -920,15 +920,17 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
return DeleteNav(this.Queryable<T>().Where(whereExpression).ToList());
|
return DeleteNav(this.Queryable<T>().Where(whereExpression).ToList());
|
||||||
}
|
}
|
||||||
public UpdateNavProvider<T, T> UpdateNav<T>(T data)
|
public UpdateNavTaskInit<T, T> UpdateNav<T>(T data) where T : class, new()
|
||||||
{
|
{
|
||||||
return UpdateNav(new List<T>() { data });
|
return UpdateNav(new List<T>() { data });
|
||||||
}
|
}
|
||||||
public UpdateNavProvider<T, T> UpdateNav<T>(List<T> datas)
|
public UpdateNavTaskInit<T, T> UpdateNav<T>(List<T> datas) where T : class, new()
|
||||||
{
|
{
|
||||||
var result = new UpdateNavProvider<T, T>();
|
var result = new UpdateNavTaskInit<T, T>();
|
||||||
result.Roots = datas;
|
var provider = new UpdateNavProvider<T, T>();
|
||||||
result.Context = this;
|
provider._Roots = datas;
|
||||||
|
provider._Context = this;
|
||||||
|
result.UpdateNavProvider = provider;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
@ -698,11 +698,11 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
return ScopedContext.DeleteNav(whereExpression);
|
return ScopedContext.DeleteNav(whereExpression);
|
||||||
}
|
}
|
||||||
public UpdateNavProvider<T, T> UpdateNav<T>(T data)
|
public UpdateNavTaskInit<T, T> UpdateNav<T>(T data) where T : class, new()
|
||||||
{
|
{
|
||||||
return ScopedContext.UpdateNav(data);
|
return ScopedContext.UpdateNav(data);
|
||||||
}
|
}
|
||||||
public UpdateNavProvider<T, T> UpdateNav<T>(List<T> datas)
|
public UpdateNavTaskInit<T, T> UpdateNav<T>(List<T> datas) where T : class, new()
|
||||||
{
|
{
|
||||||
return ScopedContext.UpdateNav(datas);
|
return ScopedContext.UpdateNav(datas);
|
||||||
}
|
}
|
||||||
|
@ -208,8 +208,8 @@ namespace SqlSugar
|
|||||||
DeleteNavTaskInit<T, T> DeleteNav<T>(T data) where T : class, new();
|
DeleteNavTaskInit<T, T> DeleteNav<T>(T data) where T : class, new();
|
||||||
DeleteNavTaskInit<T, T> DeleteNav<T>(List<T> datas) where T : class, new();
|
DeleteNavTaskInit<T, T> DeleteNav<T>(List<T> datas) where T : class, new();
|
||||||
DeleteNavTaskInit<T, T> DeleteNav<T>(Expression<Func<T,bool>> whereExpression) where T : class, new();
|
DeleteNavTaskInit<T, T> DeleteNav<T>(Expression<Func<T,bool>> whereExpression) where T : class, new();
|
||||||
UpdateNavProvider<T, T> UpdateNav<T>(T data);
|
UpdateNavTaskInit<T, T> UpdateNav<T>(T data) where T : class, new ();
|
||||||
UpdateNavProvider<T, T> UpdateNav<T>(List<T> datas);
|
UpdateNavTaskInit<T, T> UpdateNav<T>(List<T> datas) where T : class, new ();
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -89,6 +89,11 @@
|
|||||||
<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\UpdateNavOneToOne.cs" />
|
||||||
|
<Compile Include="Abstract\ExecuteNavProvider\UpdateNavManyToMany.cs" />
|
||||||
|
<Compile Include="Abstract\ExecuteNavProvider\UpdateNavOneToMany.cs" />
|
||||||
|
<Compile Include="Abstract\ExecuteNavProvider\UpdateNavProviderHelper.cs" />
|
||||||
|
<Compile Include="Abstract\ExecuteNavProvider\UpdateNavProvider.cs" />
|
||||||
<Compile Include="Abstract\ExecuteNavProvider\DeleteNavManyToMany.cs" />
|
<Compile Include="Abstract\ExecuteNavProvider\DeleteNavManyToMany.cs" />
|
||||||
<Compile Include="Abstract\ExecuteNavProvider\DeleteNavOneToMany.cs" />
|
<Compile Include="Abstract\ExecuteNavProvider\DeleteNavOneToMany.cs" />
|
||||||
<Compile Include="Abstract\ExecuteNavProvider\DeleteNavOneToOne.cs" />
|
<Compile Include="Abstract\ExecuteNavProvider\DeleteNavOneToOne.cs" />
|
||||||
@ -99,7 +104,7 @@
|
|||||||
<Compile Include="Abstract\ExecuteNavProvider\InsertNavProviderOneToOne.cs" />
|
<Compile Include="Abstract\ExecuteNavProvider\InsertNavProviderOneToOne.cs" />
|
||||||
<Compile Include="Abstract\ExecuteNavProvider\DeleteNavTask.cs" />
|
<Compile Include="Abstract\ExecuteNavProvider\DeleteNavTask.cs" />
|
||||||
<Compile Include="Abstract\ExecuteNavProvider\InsertNavTask.cs" />
|
<Compile Include="Abstract\ExecuteNavProvider\InsertNavTask.cs" />
|
||||||
<Compile Include="Abstract\ExecuteNavProvider\UpdateInsert.cs" />
|
<Compile Include="Abstract\ExecuteNavProvider\UpdateNavTask.cs" />
|
||||||
<Compile Include="Abstract\ExecuteNavProvider\InsertNavProvider.cs" />
|
<Compile Include="Abstract\ExecuteNavProvider\InsertNavProvider.cs" />
|
||||||
<Compile Include="Abstract\ExpressionableProvider\Expressionable.cs" />
|
<Compile Include="Abstract\ExpressionableProvider\Expressionable.cs" />
|
||||||
<Compile Include="Abstract\FastestProvider\FastBuilder.cs" />
|
<Compile Include="Abstract\FastestProvider\FastBuilder.cs" />
|
||||||
|
@ -174,11 +174,11 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
return this.Context.DeleteNav(whereExpression);
|
return this.Context.DeleteNav(whereExpression);
|
||||||
}
|
}
|
||||||
public UpdateNavProvider<T, T> UpdateNav<T>(T data)
|
public UpdateNavTaskInit<T, T> UpdateNav<T>(T data) where T : class, new()
|
||||||
{
|
{
|
||||||
return this.Context.UpdateNav(data);
|
return this.Context.UpdateNav(data);
|
||||||
}
|
}
|
||||||
public UpdateNavProvider<T, T> UpdateNav<T>(List<T> datas)
|
public UpdateNavTaskInit<T, T> UpdateNav<T>(List<T> datas) where T : class, new()
|
||||||
{
|
{
|
||||||
return this.Context.UpdateNav(datas);
|
return this.Context.UpdateNav(datas);
|
||||||
}
|
}
|
||||||
|
@ -754,11 +754,11 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
return ScopedContext.DeleteNav(whereExpression);
|
return ScopedContext.DeleteNav(whereExpression);
|
||||||
}
|
}
|
||||||
public UpdateNavProvider<T, T> UpdateNav<T>(T data)
|
public UpdateNavTaskInit<T, T> UpdateNav<T>(T data) where T : class, new()
|
||||||
{
|
{
|
||||||
return ScopedContext.UpdateNav(data);
|
return ScopedContext.UpdateNav(data);
|
||||||
}
|
}
|
||||||
public UpdateNavProvider<T, T> UpdateNav<T>(List<T> datas)
|
public UpdateNavTaskInit<T, T> UpdateNav<T>(List<T> datas) where T : class, new()
|
||||||
{
|
{
|
||||||
return ScopedContext.UpdateNav(datas);
|
return ScopedContext.UpdateNav(datas);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user