mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-05 17:37:58 +08:00
Navigation Insert middle table supports additional fields
This commit is contained in:
parent
0f180cd354
commit
03b6c1d677
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
@ -9,7 +10,8 @@ namespace SqlSugar
|
||||
public partial class InsertNavProvider<Root, T> where T : class, new() where Root : class, new()
|
||||
{
|
||||
private void InsertManyToMany<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
|
||||
{;
|
||||
{
|
||||
;
|
||||
var parentEntity = _ParentEntity;
|
||||
var parentList = _ParentList;
|
||||
var parentPkColumn = parentEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true);
|
||||
@ -18,9 +20,9 @@ namespace SqlSugar
|
||||
var thisPkColumn = thisEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true);
|
||||
Check.ExceptionEasy(thisPkColumn == null, $"{thisPkColumn.EntityName} need primary key", $"{thisPkColumn.EntityName}需要主键");
|
||||
Check.ExceptionEasy(parentPkColumn == null, $"{parentPkColumn.EntityName} need primary key", $"{parentPkColumn.EntityName}需要主键");
|
||||
var mappingType=parentNavigateProperty.Navigat.MappingType;
|
||||
var mappingType = parentNavigateProperty.Navigat.MappingType;
|
||||
var mappingEntity = this._Context.EntityMaintenance.GetEntityInfo(mappingType);
|
||||
var mappingA = mappingEntity.Columns.FirstOrDefault(x=>x.PropertyName== parentNavigateProperty.Navigat.MappingAId);
|
||||
var mappingA = mappingEntity.Columns.FirstOrDefault(x => x.PropertyName == parentNavigateProperty.Navigat.MappingAId);
|
||||
var mappingB = mappingEntity.Columns.FirstOrDefault(x => x.PropertyName == parentNavigateProperty.Navigat.MappingBId);
|
||||
Check.ExceptionEasy(mappingA == null || mappingB == null, $"Navigate property {name} error ", $"导航属性{name}配置错误");
|
||||
var mappingPk = mappingEntity.Columns
|
||||
@ -37,18 +39,18 @@ namespace SqlSugar
|
||||
List<Dictionary<string, object>> mappgingTables = new List<Dictionary<string, object>>();
|
||||
foreach (var item in parentList)
|
||||
{
|
||||
var items= parentNavigateProperty.PropertyInfo.GetValue(item);
|
||||
var items = parentNavigateProperty.PropertyInfo.GetValue(item);
|
||||
if (items == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
var children=((List<TChild>)items);
|
||||
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>();
|
||||
Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
|
||||
keyValuePairs.Add(mappingA.DbColumnName, parentId);
|
||||
keyValuePairs.Add(mappingB.DbColumnName, chidId);
|
||||
if (mappingOthers != null)
|
||||
@ -72,7 +74,7 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
}
|
||||
if (mappingPk != null)
|
||||
if (mappingPk != null)
|
||||
{
|
||||
SetMappingTableDefaultValue(mappingPk, keyValuePairs);
|
||||
}
|
||||
@ -88,10 +90,62 @@ namespace SqlSugar
|
||||
{
|
||||
this._Context.Deleteable<object>().AS(mappingEntity.DbTableName).In(mappingA.DbColumnName, ids).ExecuteCommand();
|
||||
}
|
||||
this._Context.Insertable(mappgingTables).AS(mappingEntity.DbTableName).ExecuteCommand();
|
||||
if (HasMappingTemplate(mappingEntity))
|
||||
{
|
||||
InertMappingWithTemplate(mappingEntity, mappingA, mappingB, mappgingTables);
|
||||
}
|
||||
else
|
||||
{
|
||||
this._Context.Insertable(mappgingTables).AS(mappingEntity.DbTableName).ExecuteCommand();
|
||||
}
|
||||
SetNewParent<TChild>(thisEntity, thisPkColumn);
|
||||
}
|
||||
|
||||
private void InertMappingWithTemplate(EntityInfo mappingEntity, EntityColumnInfo mappingA, EntityColumnInfo mappingB, List<Dictionary<string, object>> mappgingTables)
|
||||
{
|
||||
var template = this._navOptions?.ManyToManySaveMappingTemplate;
|
||||
List<object> mappingObjects = new List<object>();
|
||||
foreach (var item in mappgingTables)
|
||||
{
|
||||
// 序列化模板对象
|
||||
var serializedTemplate = JsonConvert.SerializeObject(template);
|
||||
|
||||
// 反序列化模板对象,创建新的映射对象
|
||||
var mappingObject = JsonConvert.DeserializeObject(serializedTemplate, template.GetType());
|
||||
|
||||
// 获取映射对象的所有字段
|
||||
var fields = mappingEntity.Columns;
|
||||
|
||||
// 遍历字典中的键值对,并将值赋给映射对象的对应字段
|
||||
foreach (var kvp in item)
|
||||
{
|
||||
var fieldName = kvp.Key;
|
||||
var fieldValue = kvp.Value;
|
||||
|
||||
// 查找与字段名匹配的字段
|
||||
var field = fields.FirstOrDefault(f => f.DbColumnName.EqualCase(fieldName));
|
||||
// 如果字段存在且值的类型与字段类型匹配,则赋值给字段
|
||||
if (field != null)
|
||||
{
|
||||
var isSetValue = field.IsPrimarykey
|
||||
|| field.DbColumnName == mappingA.DbColumnName
|
||||
|| field.DbColumnName == mappingB.DbColumnName;
|
||||
if (isSetValue)
|
||||
field.PropertyInfo.SetValue(mappingObject, fieldValue);
|
||||
}
|
||||
}
|
||||
|
||||
// 将映射对象添加到列表中
|
||||
mappingObjects.Add(mappingObject);
|
||||
}
|
||||
this._Context.InsertableByObject(mappingObjects).ExecuteCommand();
|
||||
}
|
||||
|
||||
private bool HasMappingTemplate(EntityInfo mappingEntity)
|
||||
{
|
||||
return this._navOptions?.ManyToManySaveMappingTemplate?.GetType() == mappingEntity.Type;
|
||||
}
|
||||
|
||||
private void SetMappingTableDefaultValue(EntityColumnInfo mappingPk, Dictionary<string, object> keyValuePairs)
|
||||
{
|
||||
if (mappingPk.UnderType == UtilConstants.LongType)
|
||||
|
@ -43,6 +43,7 @@ namespace SqlSugar
|
||||
{
|
||||
public bool ManyToManyIsUpdateA { get; set; }
|
||||
public bool ManyToManyIsUpdateB { get; set; }
|
||||
public object ManyToManySaveMappingTemplate { get; set; }
|
||||
public bool OneToManyDeleteAll { get; set; }
|
||||
public bool OneToManyEnableLogicDelete { get; set; }
|
||||
public Expression RootFunc { get; set; }
|
||||
@ -53,5 +54,6 @@ namespace SqlSugar
|
||||
{
|
||||
public bool OneToManyIfExistsNoInsert { get; set; }
|
||||
public bool ManyToManyNoDeleteMap { get; set; }
|
||||
public object ManyToManySaveMappingTemplate { get; set; }
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user