Browse Source

Insert many to may

pull/20/head
sunkaixuan 2 years ago
parent
commit
e5ceb95bc0
  1. 7
      Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs
  2. 2
      Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderHelper.cs
  3. 60
      Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderManyToMany.cs
  4. 1
      Src/Asp.Net/SqlSugar/Interface/IDeleteable.cs

7
Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs

@ -439,6 +439,13 @@ namespace SqlSugar
tempPrimaryKeys = null;
return this;
}
public IDeleteable<T> In<PkType>(string inField, List<PkType> primaryKeyValues)
{
tempPrimaryKeys = new List<string>() { inField };
var result = In(primaryKeyValues);
tempPrimaryKeys = null;
return this;
}
public IDeleteable<T> With(string lockString)
{

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

@ -128,7 +128,7 @@ namespace SqlSugar
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}不是可自动赋值类型,需要赋值");
Check.ExceptionEasy($"The field {name} is not an autoassignment type and requires an assignment", $"字段{name}不是可自动赋值类型,需要赋值 , 可赋值类型有 自增、long、Guid、string");
}
}
this._Context.Insertable(insertData).ExecuteCommand();

60
Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderManyToMany.cs

@ -9,8 +9,68 @@ 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);
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<object, object>> mappgingTables = new List<Dictionary<object, 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);
}
}
}
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。你也可以删掉主键 用双主键");
}
}
}
}

1
Src/Asp.Net/SqlSugar/Interface/IDeleteable.cs

@ -25,6 +25,7 @@ namespace SqlSugar
IDeleteable<T> In<PkType>(Expression<Func<T,object>> inField,PkType primaryKeyValue);
IDeleteable<T> In<PkType>(Expression<Func<T, object>> inField,PkType[] primaryKeyValues);
IDeleteable<T> In<PkType>(Expression<Func<T, object>> inField,List<PkType> primaryKeyValues);
IDeleteable<T> In<PkType>(string inField, List<PkType> primaryKeyValues);
IDeleteable<T> Where(string whereString,object parameters=null);
IDeleteable<T> Where(string whereString, SugarParameter parameter);
IDeleteable<T> Where(string whereString, SugarParameter[] parameters);

Loading…
Cancel
Save